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 1.8KB

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