discard """ output: ''' done tableadds And we get here 1 2 3 ''' joinable: false """ import hashes, sequtils, tables, algorithm proc sortedPairs[T](t: T): auto = toSeq(t.pairs).sorted template sortedItems(t: untyped): untyped = sorted(toSeq(t)) block tableDollar: # other tests should use `sortedPairs` to be robust to future table/hash # implementation changes doAssert ${1: 'a', 2: 'b'}.toTable in ["{1: 'a', 2: 'b'}", "{2: 'b', 1: 'a'}"] # test should not be joined because it takes too long. block tableadds: proc main = var tab = newTable[string, string]() for i in 0..1000: tab.add "key", "value " & $i main() echo "done tableadds" block tcounttable: # bug #2625 const s_len = 32 var substr_counts: CountTable[string] = initCountTable[string]() var my_string = "Hello, this is sadly broken for strings over 64 characters. Note that it *does* appear to work for short strings." for i in 0..(my_string.len - s_len): let s = my_string[i..i+s_len-1] substr_counts[s] = 1 # substr_counts[s] = substr_counts[s] + 1 # Also breaks, + 2 as well, etc. # substr_counts.inc(s) # This works #echo "Iteration ", i echo "And we get here" block thashes: # Test with int block: var t = initTable[int,int]() t[0] = 42 t[1] = t[0] + 1 assert(t[0] == 42) assert(t[1] == 43) let t2 = {1: 1, 2: 2}.toTable assert(t2[2] == 2) # Test with char block: var t = initTable[char,int]() t['0'] = 42 t['1'] = t['0'] + 1 assert(t['0'] == 42) assert(t['1'] == 43) let t2 = {'1': 1, '2': 2}.toTable assert(t2['2'] == 2) # Test with enum block: type E = enum eA, eB, eC var t = initTable[E,int]() t[eA] = 42 t[eB] = t[eA] + 1 assert(t[eA] == 42) assert(t[eB] == 43) let t2 = {eA: 1, eB: 2}.toTable assert(t2[eB] == 2) # Test with range block: type R = range[0..9] var t = initTable[R,int]() # causes warning, why? t[1] = 42 # causes warning, why? t[2] = t[1] + 1 assert(t[1] == 42) assert(t[2] == 43) let t2 = {1.R: 1, 2.R: 2}.toTable assert(t2[2.R] == 2) # Test which combines the generics for tuples + ordinals block: type E = enum eA, eB, eC var t = initTable[(string, E, int, char), int]() t[("a", eA, 0, '0')] = 42 t[("b", eB, 1, '1')] = t[("a", eA, 0, '0')] + 1 assert(t[("a", eA, 0, '0')] == 42) assert(t[("b", eB, 1, '1')] == 43) let t2 = {("a", eA, 0, '0'): 1, ("b", eB, 1, '1'): 2}.toTable assert(t2[("b", eB, 1, '1')] == 2) # Test to check if overloading is possible # Unfortunately, this does not seem to work for int # The same test with a custom hash(s: string) does # work though. block: proc hash(x: int): Hash {.inline.} = echo "overloaded hash" result = x var t = initTable[int, int]() t[0] = 0 # Check hashability of all integer types (issue #5429) block: let intTables = ( newTable[int, string](), newTable[int8, string](), newTable[int16, string](), newTable[int32, string](), newTable[int64, string](), newTable[uint, string](), newTable[uint8, string](), newTable[uint16, string](), newTable[uint32, string](), newTable[uint64, string](), ) echo "1" block tindexby: doAssert indexBy(newSeq[int](), proc(x: int):int = x) == initTable[int, int](), "empty int table" var tbl1 = initTable[int, int]() tbl1.add(1,1) tbl1.add(2,2) doAssert indexBy(@[1,2], proc(x: int):int = x) == tbl1, "int table" type TElem = object foo: int bar: string let elem1 = TElem(foo: 1, bar: "bar") elem2 = TElem(foo: 2, bar: "baz") var tbl2 = initTable[string, TElem]() tbl2.add("bar", elem1) tbl2.add("baz", elem2) doAssert indexBy(@[elem1,elem2], proc(x: TElem): string = x.bar) == tbl2, "element table" block tableconstr: # Test if the new table constructor syntax works: template ignoreExpr(e) = discard # test first class '..' syntactical citizen: ignoreExpr x <> 2..4 # test table constructor: ignoreExpr({:}) ignoreExpr({2: 3, "key": "value"}) # NEW: assert 56 in 50..100 assert 56 in ..60 block ttables2: proc TestHashIntInt() = var tab = initTable[int,int]() let n = 100_000 for i in 1..n: tab[i] = i for i in 1..n: var x = tab[i] if x != i : echo "not found ", i proc run1() = for i in 1 .. 50: TestHashIntInt() # bug #2107 var delTab = initTable[int,int](4) for i in 1..4: delTab[i] = i delTab.del(i) delTab[5] = 5 run1() echo "2" block allValues: var t: Table[int, string] var key = 0 let n = 1000 for i in 0..