|
| 1 | +package javascript_helper |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestEndianConversions(t *testing.T) { |
| 8 | + conv := &Convert{} |
| 9 | + |
| 10 | + // test data |
| 11 | + leBytes16 := []byte{0x34, 0x12} // 0x1234 |
| 12 | + leBytes32 := []byte{0x78, 0x56, 0x34, 0x12} // 0x12345678 |
| 13 | + leBytes64 := []byte{0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12} // 0x123456789ABCDEF0 |
| 14 | + |
| 15 | + beBytes16 := []byte{0x12, 0x34} |
| 16 | + beBytes32 := []byte{0x12, 0x34, 0x56, 0x78} |
| 17 | + beBytes64 := []byte{0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0} |
| 18 | + |
| 19 | + t.Run("ToUInt16LE", func(t *testing.T) { |
| 20 | + want := uint16(0x1234) |
| 21 | + got := conv.ToUInt16LE(leBytes16) |
| 22 | + if got != want { |
| 23 | + t.Errorf("ToUInt16LE() = %d, want %d", got, want) |
| 24 | + } |
| 25 | + }) |
| 26 | + |
| 27 | + t.Run("ToUInt32LE", func(t *testing.T) { |
| 28 | + want := uint32(0x12345678) |
| 29 | + got := conv.ToUInt32LE(leBytes32) |
| 30 | + if got != want { |
| 31 | + t.Errorf("ToUInt32LE() = %d, want %d", got, want) |
| 32 | + } |
| 33 | + }) |
| 34 | + |
| 35 | + t.Run("ToUInt64LE", func(t *testing.T) { |
| 36 | + want := uint64(0x123456789ABCDEF0) |
| 37 | + got := conv.ToUInt64LE(leBytes64) |
| 38 | + if got != want { |
| 39 | + t.Errorf("ToUInt64LE() = %d, want %d", got, want) |
| 40 | + } |
| 41 | + }) |
| 42 | + |
| 43 | + t.Run("ToUInt16BE", func(t *testing.T) { |
| 44 | + want := uint16(0x1234) |
| 45 | + got := conv.ToUInt16BE(beBytes16) |
| 46 | + if got != want { |
| 47 | + t.Errorf("ToUInt16BE() = %d, want %d", got, want) |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + t.Run("ToUInt32BE", func(t *testing.T) { |
| 52 | + want := uint32(0x12345678) |
| 53 | + got := conv.ToUInt32BE(beBytes32) |
| 54 | + if got != want { |
| 55 | + t.Errorf("ToUInt32BE() = %d, want %d", got, want) |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("ToUInt64BE", func(t *testing.T) { |
| 60 | + want := uint64(0x123456789ABCDEF0) |
| 61 | + got := conv.ToUInt64BE(beBytes64) |
| 62 | + if got != want { |
| 63 | + t.Errorf("ToUInt64BE() = %d, want %d", got, want) |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("ToInt16LE", func(t *testing.T) { |
| 68 | + bytes := []byte{0xFF, 0x7F} // 0x7FFF -> 32767 |
| 69 | + want := int16(32767) |
| 70 | + got := conv.ToInt16LE(bytes) |
| 71 | + if got != want { |
| 72 | + t.Errorf("ToInt16LE() = %d, want %d", got, want) |
| 73 | + } |
| 74 | + |
| 75 | + bytes = []byte{0x00, 0x80} // 0x8000 -> -32768 |
| 76 | + want = -32768 |
| 77 | + got = conv.ToInt16LE(bytes) |
| 78 | + if got != want { |
| 79 | + t.Errorf("ToInt16LE() = %d, want %d", got, want) |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("ToInt32BE", func(t *testing.T) { |
| 84 | + bytes := []byte{0xFF, 0xFF, 0xFF, 0xFF} // -1 |
| 85 | + want := int32(-1) |
| 86 | + got := conv.ToInt32BE(bytes) |
| 87 | + if got != want { |
| 88 | + t.Errorf("ToInt32BE() = %d, want %d", got, want) |
| 89 | + } |
| 90 | + }) |
| 91 | +} |
0 commit comments