tsharedtable.nim 1.7 KB

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