您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

bitset_test.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. 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. }