tcritbits.nim 1.6 KB

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