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.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. BitsetSet(t1s, i, true)
  17. }
  18. }
  19. if !(BitsetGet(t1s, 0) && !BitsetGet(t1s, 1) && BitsetGet(t1s, 64) && BitsetGet(t1s, 72) && !BitsetGet(t1s, 127)) {
  20. t.Error("exactly the even-numbered bits should be set")
  21. }
  22. BitsetSet(t1s, 72, false)
  23. if BitsetGet(t1s, 72) {
  24. t.Error("remove doesn't work")
  25. }
  26. var t2 testBitset
  27. t2s := t2[:]
  28. BitsetInitialize(t2s)
  29. for i = 0; i < 128; i++ {
  30. if i%2 == 1 {
  31. BitsetSet(t2s, i, true)
  32. }
  33. }
  34. BitsetUnion(t1s, t2s)
  35. for i = 0; i < 128; i++ {
  36. expected := (i != 72)
  37. if BitsetGet(t1s, i) != expected {
  38. t.Error("all bits should be set except 72")
  39. }
  40. }
  41. }