summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorSimon Hafner <hafnersimon@gmail.com>2014-02-06 16:11:55 -0600
committerSimon Hafner <hafnersimon@gmail.com>2014-02-06 16:11:55 -0600
commit5498415f3b44048739c9b7116638824713d9c1df (patch)
tree5d010ada05ec74ffb2b981d1be326db9ca5fbda6 /tests
parent75f232eb6efa1d213ca84c524ee9119da128d6e3 (diff)
downloadNim-5498415f3b44048739c9b7116638824713d9c1df.tar.gz
indexBy, which indexes a collection into a hashtable
Diffstat (limited to 'tests')
-rw-r--r--tests/collections/ttables.nim22
1 files changed, 22 insertions, 0 deletions
diff --git a/tests/collections/ttables.nim b/tests/collections/ttables.nim
new file mode 100644
index 000000000..f374d5504
--- /dev/null
+++ b/tests/collections/ttables.nim
@@ -0,0 +1,22 @@
+import tables
+
+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"
d='n155' href='#n155'>155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200