state_test.go raw

   1  package state
   2  
   3  import "testing"
   4  
   5  func TestTrigramBits(t *testing.T) {
   6  	tests := []struct {
   7  		tri                            Trigram
   8  		name                           string
   9  		bonding, constraint, energy    bool
  10  	}{
  11  		{Earth, "Earth", false, false, false},
  12  		{Thunder, "Thunder", true, false, false},
  13  		{Water, "Water", false, true, false},
  14  		{Lake, "Lake", true, true, false},
  15  		{Fire, "Fire", false, false, true},
  16  		{Heaven, "Heaven", true, false, true},
  17  		{Wind, "Wind", false, true, true},
  18  		{Mountain, "Mountain", true, true, true},
  19  	}
  20  	for _, tt := range tests {
  21  		if tt.tri.Bonding() != tt.bonding {
  22  			t.Errorf("%s: Bonding() = %v, want %v", tt.name, tt.tri.Bonding(), tt.bonding)
  23  		}
  24  		if tt.tri.Constraint() != tt.constraint {
  25  			t.Errorf("%s: Constraint() = %v, want %v", tt.name, tt.tri.Constraint(), tt.constraint)
  26  		}
  27  		if tt.tri.Energy() != tt.energy {
  28  			t.Errorf("%s: Energy() = %v, want %v", tt.name, tt.tri.Energy(), tt.energy)
  29  		}
  30  	}
  31  }
  32  
  33  func TestTrigramSetters(t *testing.T) {
  34  	// Build Mountain from Earth by setting all bits.
  35  	tri := Earth
  36  	tri = tri.SetBonding(true)
  37  	tri = tri.SetConstraint(true)
  38  	tri = tri.SetEnergy(true)
  39  	if tri != Mountain {
  40  		t.Errorf("expected Mountain (111), got %03b", tri)
  41  	}
  42  
  43  	// Clear back to Earth.
  44  	tri = tri.SetBonding(false)
  45  	tri = tri.SetConstraint(false)
  46  	tri = tri.SetEnergy(false)
  47  	if tri != Earth {
  48  		t.Errorf("expected Earth (000), got %03b", tri)
  49  	}
  50  }
  51  
  52  func TestFlip(t *testing.T) {
  53  	// Earth (000) flip bonding -> Thunder (001)
  54  	if Earth.Flip(BitBonding) != Thunder {
  55  		t.Error("Earth flip bonding should be Thunder")
  56  	}
  57  	// Heaven (101) flip constraint -> Mountain (111)
  58  	if Heaven.Flip(BitConstraint) != Mountain {
  59  		t.Error("Heaven flip constraint should be Mountain")
  60  	}
  61  	// Mountain (111) flip energy -> Lake (011)
  62  	if Mountain.Flip(BitEnergy) != Lake {
  63  		t.Error("Mountain flip energy should be Lake")
  64  	}
  65  }
  66  
  67  func TestHexagramRoundTrip(t *testing.T) {
  68  	for i := uint8(0); i < 64; i++ {
  69  		h := Hexagram(i)
  70  		got := uint8(Hex(h.Inner(), h.Outer()))
  71  		if got != i {
  72  			t.Errorf("hexagram roundtrip failed: %d -> inner=%03b outer=%03b -> %d", i, h.Inner(), h.Outer(), got)
  73  		}
  74  	}
  75  }
  76  
  77  func TestMoveLine(t *testing.T) {
  78  	// Creative (Heaven/Heaven) move inner bonding line
  79  	// Heaven = 101, flip bit 0 (bonding) -> 100 = Fire
  80  	creative := Hex(Heaven, Heaven)
  81  	moved := creative.MoveLine(true, BitBonding)
  82  	if moved.Inner() != Fire {
  83  		t.Errorf("expected Fire inner, got %03b", moved.Inner())
  84  	}
  85  	if moved.Outer() != Heaven {
  86  		t.Errorf("outer should be unchanged, got %03b", moved.Outer())
  87  	}
  88  }
  89