1 package minecraft 2 3 import ( 4 "testing" 5 6 "vimagination.zapto.org/minecraft/nbt" 7 ) 8 9 func TestY(t *testing.T) { 10 section := newSection(0) 11 if y := section.section.Get("Y").Data().(nbt.Byte); y != 0 { 12 t.Errorf("expecting %d, got %d", 0, y) 13 } 14 section = newSection(16) 15 if y := section.section.Get("Y").Data().(nbt.Byte); y != 1 { 16 t.Errorf("expecting %d, got %d", 1, y) 17 } 18 section.SetY(48) 19 if y := section.section.Get("Y").Data().(nbt.Byte); y != 3 { 20 t.Errorf("expecting %d, got %d", 3, y) 21 } 22 section.SetY(255) 23 if y := section.section.Get("Y").Data().(nbt.Byte); y != 15 { 24 t.Errorf("expecting %d, got %d", 15, y) 25 } 26 } 27 28 func TestGetBlock(t *testing.T) { 29 blocks := make(nbt.ByteArray, 4096) 30 add := make(nbt.ByteArray, 2048) 31 data := make(nbt.ByteArray, 2048) 32 blocks[0] = 1 33 blocks[10] = 2 34 blocks[18] = 24 35 blocks[19] = 13 36 blocks[3475] = 45 37 blocks[4054] = b2i(194) 38 add[9] = b2i(12<<4 | 7) 39 add[2027] = 5 40 data[1737] = b2i(9 << 4) 41 data[2027] = 8 42 section, _ := loadSection(nbt.Compound{ 43 nbt.NewTag("Blocks", blocks), 44 nbt.NewTag("Add", add), 45 nbt.NewTag("Data", data), 46 nbt.NewTag("BlockLight", make(nbt.ByteArray, 2048)), 47 nbt.NewTag("SkyLight", make(nbt.ByteArray, 2048)), 48 nbt.NewTag("Y", nbt.Byte(0)), 49 }) 50 tests := []struct { 51 xyz [3]int32 52 Block 53 }{ 54 {[3]int32{0, 0, 0}, Block{ID: 1}}, 55 {[3]int32{10, 0, 0}, Block{ID: 2}}, 56 {[3]int32{2, 0, 1}, Block{ID: 1816}}, 57 {[3]int32{3, 0, 1}, Block{ID: 3085}}, 58 {[3]int32{3, 13, 9}, Block{ID: 45, Data: 9}}, 59 {[3]int32{6, 15, 13}, Block{ID: 1474, Data: 8}}, 60 {[3]int32{9, 12, 11}, Block{}}, 61 } 62 for n, test := range tests { 63 if b := section.GetBlock(test.xyz[0], test.xyz[1], test.xyz[2]); !test.Block.EqualBlock(b) { 64 t.Errorf("test %d failed\nExpecting: %s\nGot: %s", n+1, test.Block.String(), b.String()) 65 } 66 } 67 } 68 69 func TestSetBlock(t *testing.T) { 70 section := newSection(32) 71 tests := []struct { 72 xyz [3]int32 73 Block 74 recheck bool 75 }{ 76 {[3]int32{4, 7, 9}, Block{ID: 12, Data: 4}, false}, 77 {[3]int32{4, 7, 9}, Block{ID: 15}, false}, 78 {[3]int32{1, 12, 10}, Block{ID: 1345, Data: 12}, true}, 79 {[3]int32{2, 12, 10}, Block{ID: 765, Data: 5}, true}, 80 {[3]int32{3, 12, 10}, Block{ID: 451, Data: 11}, false}, 81 {[3]int32{4, 7, 9}, Block{ID: 761, Data: 5}, false}, 82 {[3]int32{4, 7, 9}, Block{}, false}, 83 } 84 for n, test := range tests { 85 section.SetBlock(test.xyz[0], test.xyz[1], test.xyz[2], test.Block) 86 if b := section.GetBlock(test.xyz[0], test.xyz[1], test.xyz[2]); !test.Block.EqualBlock(b) { 87 t.Errorf("test %d failed\nExpecting: %s\nGot: %s", n+1, test.Block.String(), b.String()) 88 } 89 } 90 for n, test := range tests { 91 if test.recheck { 92 if b := section.GetBlock(test.xyz[0], test.xyz[1], test.xyz[2]); !test.Block.EqualBlock(b) { 93 t.Errorf("retest %d failed\nExpecting: %s\nGot: %s", n+1, test.Block.String(), b.String()) 94 } 95 } 96 } 97 } 98 99 func b2i(b byte) int8 { 100 return int8(b) 101 } 102