tcritbits.nim 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. targets: "c js"
  4. """
  5. import std/[sequtils,critbits]
  6. import std/assertions
  7. template main =
  8. var r: CritBitTree[void]
  9. r.incl "abc"
  10. r.incl "xyz"
  11. r.incl "def"
  12. r.incl "definition"
  13. r.incl "prefix"
  14. r.incl "foo"
  15. doAssert r.contains"def"
  16. r.excl "def"
  17. doAssert r.missingOrExcl("foo") == false
  18. doAssert "foo" notin toSeq(r.items)
  19. doAssert r.missingOrExcl("foo") == true
  20. doAssert toSeq(r.items) == @["abc", "definition", "prefix", "xyz"]
  21. doAssert toSeq(r.itemsWithPrefix("de")) == @["definition"]
  22. var c = CritBitTree[int]()
  23. c.inc("a")
  24. doAssert c["a"] == 1
  25. c.inc("a", 4)
  26. doAssert c["a"] == 5
  27. c.inc("a", -5)
  28. doAssert c["a"] == 0
  29. c.inc("b", 2)
  30. doAssert c["b"] == 2
  31. c.inc("c", 3)
  32. doAssert c["c"] == 3
  33. c.inc("a", 1)
  34. doAssert c["a"] == 1
  35. var cf = CritBitTree[float]()
  36. cf.incl("a", 1.0)
  37. doAssert cf["a"] == 1.0
  38. cf.incl("b", 2.0)
  39. doAssert cf["b"] == 2.0
  40. cf.incl("c", 3.0)
  41. doAssert cf["c"] == 3.0
  42. doAssert cf.len == 3
  43. cf.excl("c")
  44. doAssert cf.len == 2
  45. var cb: CritBitTree[string]
  46. cb.incl("help", "help")
  47. for k in cb.keysWithPrefix("helpp"):
  48. doAssert false, "there is no prefix helpp"
  49. block: # bug #14339
  50. var strings: CritBitTree[int]
  51. discard strings.containsOrIncl("foo", 3)
  52. doAssert strings["foo"] == 3
  53. block tcritbitsToString:
  54. block:
  55. var t: CritBitTree[int]
  56. t["a"] = 1
  57. doAssert $t == """{"a": 1}"""
  58. block:
  59. var t: CritBitTree[string]
  60. t["a"] = "1"
  61. doAssert $t == """{"a": "1"}"""
  62. block:
  63. var t: CritBitTree[char]
  64. t["a"] = '1'
  65. doAssert $t == """{"a": '1'}"""
  66. main()
  67. static: main()