You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.6 KiB

  1. package keccak
  2. import (
  3. "encoding/binary"
  4. "testing"
  5. qt "github.com/frankban/quicktest"
  6. )
  7. func TestUtilsBytesToBool(t *testing.T) {
  8. b := []byte{0, 1, 2, 3, 255, 254, 253}
  9. bits := bytesToBits(b)
  10. // for i := 0; i < len(b); i++ {
  11. // fmt.Println(i*8, i*8+8, b[i], bits[i*8:i*8+8])
  12. // }
  13. qt.Assert(t, bits[0:8], qt.DeepEquals,
  14. []bool{false, false, false, false, false, false, false, false})
  15. qt.Assert(t, bits[8:16], qt.DeepEquals,
  16. []bool{true, false, false, false, false, false, false, false})
  17. qt.Assert(t, bits[16:24], qt.DeepEquals,
  18. []bool{false, true, false, false, false, false, false, false})
  19. qt.Assert(t, bits[24:32], qt.DeepEquals,
  20. []bool{true, true, false, false, false, false, false, false})
  21. qt.Assert(t, bits[32:40], qt.DeepEquals,
  22. []bool{true, true, true, true, true, true, true, true})
  23. qt.Assert(t, bits[40:48], qt.DeepEquals,
  24. []bool{false, true, true, true, true, true, true, true})
  25. qt.Assert(t, bits[48:56], qt.DeepEquals,
  26. []bool{true, false, true, true, true, true, true, true})
  27. }
  28. func TestUtilsBitsToBytes(t *testing.T) {
  29. b := []byte{0, 1, 2, 3, 255, 254, 253}
  30. bits := bytesToBits(b)
  31. b2 := bitsToBytes(bits)
  32. qt.Assert(t, b2, qt.DeepEquals, b)
  33. }
  34. func TestUtilsU64(t *testing.T) {
  35. u := uint64(100)
  36. b := make([]byte, 8)
  37. binary.LittleEndian.PutUint64(b, u)
  38. // toUint64
  39. // with bytes
  40. uRes := bytesToU64(b)
  41. qt.Assert(t, uRes, qt.Equals, u)
  42. // with bits
  43. uRes = bitsToU64(bytesToBits(b))
  44. qt.Assert(t, uRes, qt.Equals, u)
  45. // fromUint64
  46. // with bytes
  47. bRes := u64ToBytes(u)
  48. qt.Assert(t, bRes, qt.DeepEquals, b)
  49. // with bits
  50. bResBits := u64ToBits(u)
  51. qt.Assert(t, bResBits, qt.DeepEquals, bytesToBits(b))
  52. }
  53. func TestUtilsLeftShift(t *testing.T) {
  54. u := uint64(2)
  55. bits := u64ToBits(u)
  56. r := leftShift(bits, 1)
  57. qt.Assert(t, bitsToU64(r), qt.Equals, u<<1)
  58. u = uint64(9)
  59. bits = u64ToBits(u)
  60. r = leftShift(bits, 1)
  61. qt.Assert(t, bitsToU64(r), qt.Equals, u<<1)
  62. u = uint64(14)
  63. bits = u64ToBits(u)
  64. r = leftShift(bits, 63)
  65. qt.Assert(t, bitsToU64(r), qt.Equals, u<<63)
  66. u = uint64(123456)
  67. bits = u64ToBits(u)
  68. r = leftShift(bits, 1)
  69. qt.Assert(t, bitsToU64(r), qt.Equals, u<<1)
  70. }
  71. func TestUtilsRightShift(t *testing.T) {
  72. u := uint64(2)
  73. bits := u64ToBits(u)
  74. r := rightShift(bits, 1)
  75. qt.Assert(t, bitsToU64(r), qt.Equals, u>>1)
  76. u = uint64(9)
  77. bits = u64ToBits(u)
  78. r = rightShift(bits, 1)
  79. qt.Assert(t, bitsToU64(r), qt.Equals, u>>1)
  80. u = uint64(14)
  81. bits = u64ToBits(u)
  82. r = rightShift(bits, 63)
  83. qt.Assert(t, bitsToU64(r), qt.Equals, u>>63)
  84. u = uint64(123456)
  85. bits = u64ToBits(u)
  86. r = rightShift(bits, 1)
  87. qt.Assert(t, bitsToU64(r), qt.Equals, u>>1)
  88. }