diff options
author | SoniEx2 <endermoneymod@gmail.com> | 2024-06-09 13:02:25 -0300 |
---|---|---|
committer | SoniEx2 <endermoneymod@gmail.com> | 2024-06-09 13:02:25 -0300 |
commit | ca6e4d141484331a25cfa67cf370000b74d7725f (patch) | |
tree | c157a393a202044f08b02a35c5e055f3f1ba6243 /test/bucketmap-tests.cratera | |
parent | 2751039739e5357beb8e858a25e21e2bc0344fe8 (diff) |
Add hashmap implementation
Diffstat (limited to 'test/bucketmap-tests.cratera')
-rw-r--r-- | test/bucketmap-tests.cratera | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/test/bucketmap-tests.cratera b/test/bucketmap-tests.cratera new file mode 100644 index 0000000..b2e0aaa --- /dev/null +++ b/test/bucketmap-tests.cratera @@ -0,0 +1,35 @@ +local bucketlib = require "cratera.lib.bucket" + +local Bucketable = bucketlib.Bucketable +local Bucket = bucketlib.Bucket +local Struct = Struct + +local Point = mkstruct(function(_struct, x, y) + return {x, y} +end, { + __eq = function(a, b) + return a[Struct] == b[Struct] and a[1] == b[1] and a[2] == b[2] + end +}) + +Point[Bucketable] = {} +-- FIXME we really need that 'function Point:[Bucketable].bucket(bucket)' syntax... +local pointbucket = Point[Bucketable] + +function pointbucket:bucket(bucket) + bucket:[Bucket].put_number(self[1]) + bucket:[Bucket].put_number(self[2]) +end + +local map = bucketlib.BucketingMap() + +map:put(Point(0, 1), "hello") +map:put(Point(2, 3), "cratera") +map:put(Point(4, 5), "world") + +assert(map._buckets[2]) +assert(map._buckets[3]) +assert(map._buckets[4]) +assert(map:get(Point(0, 1)) == "hello") +assert(map:get(Point(2, 3)) == "cratera") +assert(map:get(Point(4, 5)) == "world") |