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.

bitset_test.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package utils
  4. import "testing"
  5. type testBitset [4]uint32
  6. func TestSets(t *testing.T) {
  7. var t1 testBitset
  8. t1s := t1[:]
  9. if BitsetGet(t1s, 0) || BitsetGet(t1s, 63) || BitsetGet(t1s, 64) || BitsetGet(t1s, 127) {
  10. t.Error("no bits should be set in a newly initialized bitset")
  11. }
  12. var i uint
  13. for i = 0; i < 128; i++ {
  14. if i%2 == 0 {
  15. if !BitsetSet(t1s, i, true) {
  16. t.Error("setting an uninitialized bit should return true")
  17. }
  18. }
  19. }
  20. if BitsetSet(t1s, 24, true) {
  21. t.Error("setting an already-set bit should return false")
  22. }
  23. if !(BitsetGet(t1s, 0) && !BitsetGet(t1s, 1) && BitsetGet(t1s, 64) && BitsetGet(t1s, 72) && !BitsetGet(t1s, 127)) {
  24. t.Error("exactly the even-numbered bits should be set")
  25. }
  26. if !BitsetSet(t1s, 72, false) {
  27. t.Error("removing a set bit should return true")
  28. }
  29. if BitsetGet(t1s, 72) {
  30. t.Error("remove doesn't work")
  31. }
  32. if BitsetSet(t1s, 72, false) {
  33. t.Error("removing an unset bit should return false")
  34. }
  35. var t2 testBitset
  36. t2s := t2[:]
  37. for i = 0; i < 128; i++ {
  38. if i%2 == 1 {
  39. BitsetSet(t2s, i, true)
  40. }
  41. }
  42. BitsetUnion(t1s, t2s)
  43. for i = 0; i < 128; i++ {
  44. expected := (i != 72)
  45. if BitsetGet(t1s, i) != expected {
  46. t.Error("all bits should be set except 72")
  47. }
  48. }
  49. var t3 testBitset
  50. t3s := t3[:]
  51. BitsetSet(t3s, 72, true)
  52. if !BitsetGet(t3s, 72) {
  53. t.Error("bit 72 should be set")
  54. }
  55. // copy t1 on top of t2
  56. BitsetCopy(t3s, t1s)
  57. for i = 0; i < 128; i++ {
  58. expected := (i != 72)
  59. if BitsetGet(t3s, i) != expected {
  60. t.Error("all bits should be set except 72")
  61. }
  62. }
  63. BitsetSubtract(t3s, t2s)
  64. if !BitsetGet(t3s, 0) || BitsetGet(t3s, 72) || !BitsetGet(t3s, 74) || BitsetGet(t3s, 71) {
  65. t.Error("subtract doesn't work")
  66. }
  67. var tlocal testBitset
  68. tlocals := tlocal[:]
  69. BitsetCopyLocal(tlocals, t1s)
  70. for i = 0; i < 128; i++ {
  71. expected := (i != 72)
  72. if BitsetGetLocal(tlocals, i) != expected {
  73. t.Error("all bits should be set except 72")
  74. }
  75. }
  76. }