tsharedtable.nim 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. discard """
  2. cmd: "nim $target --threads:on $options $file"
  3. output: '''
  4. '''
  5. """
  6. import sharedtables
  7. import std/assertions
  8. block:
  9. var table: SharedTable[int, int]
  10. init(table)
  11. table[1] = 10
  12. doAssert table.mget(1) == 10
  13. doAssert table.mgetOrPut(3, 7) == 7
  14. doAssert table.mgetOrPut(3, 99) == 7
  15. deinitSharedTable(table)
  16. import sequtils, algorithm
  17. proc sortedPairs[T](t: T): auto = toSeq(t.pairs).sorted
  18. template sortedItems(t: untyped): untyped = sorted(toSeq(t))
  19. import tables # refs issue #13504
  20. block: # we use Table as groundtruth, it's well tested elsewhere
  21. template testDel(t, t0) =
  22. template put2(i) =
  23. t[i] = i
  24. t0[i] = i
  25. template add2(i, val) =
  26. t.add(i, val)
  27. t0.add(i, val)
  28. template del2(i) =
  29. t.del(i)
  30. t0.del(i)
  31. template checkEquals() =
  32. doAssert t.len == t0.len
  33. for k,v in t0:
  34. doAssert t.mgetOrPut(k, -1) == v # sanity check
  35. doAssert t.mget(k) == v
  36. let n = 100
  37. let n2 = n*2
  38. let n3 = n*3
  39. let n4 = n*4
  40. let n5 = n*5
  41. for i in 0..<n:
  42. put2(i)
  43. for i in 0..<n:
  44. if i mod 3 == 0:
  45. del2(i)
  46. for i in n..<n2:
  47. put2(i)
  48. for i in 0..<n2:
  49. if i mod 7 == 0:
  50. del2(i)
  51. checkEquals()
  52. for i in n2..<n3:
  53. t0[i] = -2
  54. doAssert t.mgetOrPut(i, -2) == -2
  55. doAssert t.mget(i) == -2
  56. for i in 0..<n4:
  57. let ok = i in t0
  58. if not ok: t0[i] = -i
  59. doAssert t.hasKeyOrPut(i, -i) == ok
  60. checkEquals()
  61. for i in n4..<n5:
  62. add2(i, i*10)
  63. add2(i, i*11)
  64. add2(i, i*12)
  65. del2(i)
  66. del2(i)
  67. checkEquals()
  68. var t: SharedTable[int, int]
  69. init(t) # ideally should be auto-init
  70. var t0: Table[int, int]
  71. testDel(t, t0)
  72. deinitSharedTable(t)