Carp/test/map.carp

360 lines
11 KiB
Plaintext
Raw Permalink Normal View History

2018-06-11 21:50:54 +03:00
(use Map)
(load "Test.carp")
(use Test)
2018-11-07 18:11:38 +03:00
(deftest test
(assert-equal test
"2"
&(Map.get &(Map.put (Map.create) "1" "2") "1")
"basic put and get works"
)
2018-12-03 21:45:23 +03:00
(assert-equal test
"3"
&(Map.get &(Map.put (Map.put (Map.create) "1" "2") "1" "3") "1")
"put, update and get"
)
(assert-equal test
"2"
&(Map.get &(Map.put (Map.create) &-7 "2") &-7)
"basic put and get works with negative keys"
)
2019-05-22 21:13:38 +03:00
(assert-equal test
"2"
&(let-do [em (Map.create)]
(Map.put! &em "1" "2")
(Map.get &em "1"))
"put! works"
)
(assert-equal test
""
&(Map.get &(Map.create) "1")
"get works with defaults"
)
(assert-equal test
2019-03-26 12:23:27 +03:00
&(the (Maybe Int) (Maybe.Nothing))
&(Map.get-maybe &(Map.create) "1")
"get works with maybe"
)
(assert-equal test
2019-03-26 12:23:27 +03:00
&(Maybe.Just @"2")
&(Map.get-maybe &(Map.put (Map.create) "1" "2") "1")
"get works with maybe"
)
(assert-equal test
true
(Map.empty? &(Map.update (Map.create) "x" &Int.inc))
"update works with empty map"
)
(assert-equal test
2
(Map.get &(Map.update {@"x" 1} "x" &Int.inc) "x")
"update works"
)
(assert-equal test
\x ;; using char because it has no (zero) operation
(Map.get-with-default &{1 \x} &1 &\_)
"get-with-default works I"
)
(assert-equal test
\_
(Map.get-with-default &{1 \x} &2 &\_)
"get-with-default works II"
)
(assert-equal test
8
(Map.get &(Map.update-with-default (Map.create) "x" &Int.inc 7) "x")
"update-with-default works with empty map"
)
(assert-equal test
2
(Map.get &(Map.update-with-default {@"x" 1} "x" &Int.inc 7) "x")
"update-with-default works"
)
2018-11-07 18:11:38 +03:00
(assert-equal test
1
(Map.length &(Map.put (Map.create) "1" "2"))
"length works"
)
(assert-equal test
0
Support defining types in modules (BREAKING) (#1084) * fix: don't set the inner env to globals in type mods Previously, we set the inner environment of a type generated module to the global env in cases where the overarching context didn't have an inner env. This leads to problems where by the recognition of modules is inconsistent, and one can't use the names of types as submodules in certain circumstances. This commit fixes that issue. * refactor: refactor primitiveDefmodule This refactor fixes a issues with meta information on submodules, for instance, sigs on submodule functions used to result in a compiler error about ambiguous identifiers. This fixes that. Unfortunately, I don't have a precise idea about what exactly was wrong with the original definition of this function. My suspicion is that the recursion originally altered submodule paths in the wrong way, but I'm not certain. In any case it's fixed. * fix: ensure macros are expanded in the correct module Previously, macro expansions folded over all forms after the top level form, without performing any context updates on encountered `defmodules`. This created an issue in which macro calls that produced new bindings, "meta stubs", were *hoisted* out of submodules and into the top-level module, creating duplicate definitions. This commit fixes that issue by adding a special case for defmodule in macroExpand. * fix: ensure submodules and globals don't conflict Previously, our module lookups during new module definition always eventually fell back to the global environment, which caused submodules that happen to share a name with a global module to be confused with the global module. This change fixes that, so now one can define both `Dynamic` (global) and `Foo.Dynamic` without issue. * fix: remove old prefixes from vector tests Commit 7b7cb5d1e replaced /= with a generic function. However, the vector tests still called the specific Vector variants of this function, which were removed when the generic was introduced. After recent changes, these calls are now (correctly) identified as erroneous. My guess is that they only worked in the past because of problems with our lookups. * chore: format code * feat!: support defining types in modules This commit adds support for defining types (using deftype) in modules. Previously, all types were hoisted to the top level of the type environment. After this commit, the type environment supports defining nested modules just like the value env, so, calling the following: ``` (defmodule Foo (deftype Bar Baz)) ``` Adds the following to the type env: ``` Foo : Module = { Bar : Type } ``` and the following to the value env: ``` Foo : Module = { Bar : Module = { Baz : (Fn [] Foo.Bar) copy : (Fn [(Ref Foo.Bar q)] Foo.Bar) delete : (Fn [Foo.Bar] ()) get-tag : (Fn [(Ref Foo.Bar q)] Int) prn : (Fn [(Ref Foo.Bar q)] String) str : (Fn [(Ref Foo.Bar q)] String) } } ``` Such a type is *distinct* from any type defined at the top level that happens to also have the name `Bar`. This commit also updates info and tests to account for types in modules. BREAKING CHANGE: This change is breaking since it alters the names of types that were previously defined in modules. A good example of this is the `Id` type in the `Color` module. Previously, one could refer to this type by simply typing `Id` since it was hoisted to the top level. Now it *must* be referred to by `Color.Id` since `Id` at the top level of the type env and `Color.Id` (Id in the color module) are considered to be distinct types. * chore: format code * refactor: use concat instead of intercalate * chore: remove excess parentheses * chore: Add todo to return IO () in printIfFound
2020-12-22 15:27:57 +03:00
(Map.length &(the (Map Int Int) (Map.create)))
2018-11-07 18:11:38 +03:00
"length works on empty map"
)
(assert-equal test
false
Support defining types in modules (BREAKING) (#1084) * fix: don't set the inner env to globals in type mods Previously, we set the inner environment of a type generated module to the global env in cases where the overarching context didn't have an inner env. This leads to problems where by the recognition of modules is inconsistent, and one can't use the names of types as submodules in certain circumstances. This commit fixes that issue. * refactor: refactor primitiveDefmodule This refactor fixes a issues with meta information on submodules, for instance, sigs on submodule functions used to result in a compiler error about ambiguous identifiers. This fixes that. Unfortunately, I don't have a precise idea about what exactly was wrong with the original definition of this function. My suspicion is that the recursion originally altered submodule paths in the wrong way, but I'm not certain. In any case it's fixed. * fix: ensure macros are expanded in the correct module Previously, macro expansions folded over all forms after the top level form, without performing any context updates on encountered `defmodules`. This created an issue in which macro calls that produced new bindings, "meta stubs", were *hoisted* out of submodules and into the top-level module, creating duplicate definitions. This commit fixes that issue by adding a special case for defmodule in macroExpand. * fix: ensure submodules and globals don't conflict Previously, our module lookups during new module definition always eventually fell back to the global environment, which caused submodules that happen to share a name with a global module to be confused with the global module. This change fixes that, so now one can define both `Dynamic` (global) and `Foo.Dynamic` without issue. * fix: remove old prefixes from vector tests Commit 7b7cb5d1e replaced /= with a generic function. However, the vector tests still called the specific Vector variants of this function, which were removed when the generic was introduced. After recent changes, these calls are now (correctly) identified as erroneous. My guess is that they only worked in the past because of problems with our lookups. * chore: format code * feat!: support defining types in modules This commit adds support for defining types (using deftype) in modules. Previously, all types were hoisted to the top level of the type environment. After this commit, the type environment supports defining nested modules just like the value env, so, calling the following: ``` (defmodule Foo (deftype Bar Baz)) ``` Adds the following to the type env: ``` Foo : Module = { Bar : Type } ``` and the following to the value env: ``` Foo : Module = { Bar : Module = { Baz : (Fn [] Foo.Bar) copy : (Fn [(Ref Foo.Bar q)] Foo.Bar) delete : (Fn [Foo.Bar] ()) get-tag : (Fn [(Ref Foo.Bar q)] Int) prn : (Fn [(Ref Foo.Bar q)] String) str : (Fn [(Ref Foo.Bar q)] String) } } ``` Such a type is *distinct* from any type defined at the top level that happens to also have the name `Bar`. This commit also updates info and tests to account for types in modules. BREAKING CHANGE: This change is breaking since it alters the names of types that were previously defined in modules. A good example of this is the `Id` type in the `Color` module. Previously, one could refer to this type by simply typing `Id` since it was hoisted to the top level. Now it *must* be referred to by `Color.Id` since `Id` at the top level of the type env and `Color.Id` (Id in the color module) are considered to be distinct types. * chore: format code * refactor: use concat instead of intercalate * chore: remove excess parentheses * chore: Add todo to return IO () in printIfFound
2020-12-22 15:27:57 +03:00
(Map.contains? &(the (Map String Int) (Map.create)) "1")
2018-11-07 18:11:38 +03:00
"contains? works on empty map"
)
(assert-equal test
true
(Map.contains? &(Map.put (Map.create) "1" "2") "1")
"contains? works"
)
(assert-equal test
true
(Map.contains? &(Map.put (Map.create) &-7 "2") &-7)
"contains? works with negative keys"
)
(assert-equal test
false
(Map.contains? &(Map.put (Map.create) &1 "2") &-7)
"contains? works with negative keys"
)
2018-11-07 18:11:38 +03:00
(assert-equal test
true
(Map.empty? &(the (Map Int Int) (Map.create)))
"empty? works on empty map"
)
(assert-equal test
false
(Map.empty? &(Map.put (Map.create) "1" "2"))
"empty? works"
)
(assert-equal test
true
(Map.empty? &(Map.remove (Map.put (Map.create) "1" "2") "1"))
"remove works"
)
2018-12-18 17:17:08 +03:00
(assert-equal test
true
(Map.all? &(fn [k v] (or (Int.even? @k) @v)) &{1 true 2 false 4 false})
"Map.all? works I"
)
(assert-equal test
false
(Map.all? &(fn [k v] (or (Int.even? @k) @v)) &{1 true 2 false 5 false})
"Map.all? works II"
)
(assert-equal test
true
(Map.= &{1 2 3 4} &{1 2 3 4})
"Map.= works I"
)
(assert-equal test
false
(Map.= &{1 2 3 4} &{1 2 3 5})
"Map.= works II"
)
(assert-equal test
false
(Map.= &{1 2 3 4} &{1 2})
"Map.= works III"
)
(assert-equal test
true
;; 256 and 0 should hash to the same value modulo the default size
;; this means that these two maps aren't equal byte-for-byte
(Map.= &(Map.put (Map.put {} &0 &1) &256 &2)
&(Map.put (Map.put {} &256 &2) &0 &1))
"Map.= works IV"
)
2018-11-07 18:11:38 +03:00
(assert-equal test
2
(Map.length &(Map.from-array [(Pair.init 1 2)
(Pair.init 3 4)]))
2018-11-07 18:11:38 +03:00
"creating a map from an array works"
)
(assert-equal test
"{ 1 2 }"
&(str &(Map.from-array [(Pair.init 1 2)]))
2018-11-07 18:11:38 +03:00
"stringification works I"
)
(assert-equal test
"{ @\"hi\" @\"bye\" }"
&(str &(Map.from-array [(Pair.init @"hi" @"bye")]))
2018-11-07 18:11:38 +03:00
"stringification works II"
)
(assert-equal test
&[(Pair.init 1 2)]
&(Map.to-array &(Map.put (Map.create) &1 &2))
"Map.to-array works 1"
)
(assert-equal test
2
(Array.length &(Map.to-array &(Map.from-array [(Pair.init 1 2)
(Pair.init 3 4)])))
"Map.to-array works 2"
)
2018-12-07 14:22:09 +03:00
(assert-equal test
"{ 1 12 3 34 }"
&(str &(Map.endo-map &(fn [k v] (+ @v (* 10 @k)))
2018-12-07 14:22:09 +03:00
{1 2 3 4}))
"endo-map works"
)
(assert-equal test
641
(Map.kv-reduce &(fn [sum k v] (+ sum (+ (* 100 @k) (* 10 @v))))
1
&{1 1 2 1 3 2})
"kv-reduce works"
)
(assert-equal test
&[1 2 3]
&(Map.keys &{1 1 2 1 3 2})
"keys works"
)
(assert-equal test
&[1 1 2]
&(Map.vals &{1 1 2 1 3 2})
"vals works"
)
(assert-equal test
3
(Map.get &{(Pair.init 1 2) 3} &(Pair.init 1 2))
"Pairs work as keys"
)
2019-05-05 12:49:30 +03:00
(assert-equal test
&{1 @"hi" 2 @"bye"}
&(Map.reverse &{@"hi" 1 @"bye" 2})
"reverse works"
)
(assert-equal test
&{1 @"hi" 2 @"bye" 3 @"!"}
&(Map.merge {1 @"bye" 3 @"!"} &{2 @"bye" 1 @"hi"})
"merge works"
)
2019-05-22 21:13:38 +03:00
(assert-true test
(let-do [s (Set.create)]
(Set.put! &s "1")
(Set.contains? &s "1"))
"put! works"
)
(assert-equal test
1
(Set.length &(Set.put (Set.create) "1"))
"length works"
)
2018-11-07 18:11:38 +03:00
(assert-equal test
2018-12-03 21:45:23 +03:00
2
(Set.length &(Set.put (Set.put (Set.create) "1") "2"))
2018-11-07 18:11:38 +03:00
"length works"
)
2018-12-03 21:45:23 +03:00
(assert-equal test
1
(Set.length &(Set.put (Set.put (Set.create) "1") "1"))
"putting the same element twice doesn't increase size"
)
2018-11-07 18:11:38 +03:00
(assert-equal test
0
Support defining types in modules (BREAKING) (#1084) * fix: don't set the inner env to globals in type mods Previously, we set the inner environment of a type generated module to the global env in cases where the overarching context didn't have an inner env. This leads to problems where by the recognition of modules is inconsistent, and one can't use the names of types as submodules in certain circumstances. This commit fixes that issue. * refactor: refactor primitiveDefmodule This refactor fixes a issues with meta information on submodules, for instance, sigs on submodule functions used to result in a compiler error about ambiguous identifiers. This fixes that. Unfortunately, I don't have a precise idea about what exactly was wrong with the original definition of this function. My suspicion is that the recursion originally altered submodule paths in the wrong way, but I'm not certain. In any case it's fixed. * fix: ensure macros are expanded in the correct module Previously, macro expansions folded over all forms after the top level form, without performing any context updates on encountered `defmodules`. This created an issue in which macro calls that produced new bindings, "meta stubs", were *hoisted* out of submodules and into the top-level module, creating duplicate definitions. This commit fixes that issue by adding a special case for defmodule in macroExpand. * fix: ensure submodules and globals don't conflict Previously, our module lookups during new module definition always eventually fell back to the global environment, which caused submodules that happen to share a name with a global module to be confused with the global module. This change fixes that, so now one can define both `Dynamic` (global) and `Foo.Dynamic` without issue. * fix: remove old prefixes from vector tests Commit 7b7cb5d1e replaced /= with a generic function. However, the vector tests still called the specific Vector variants of this function, which were removed when the generic was introduced. After recent changes, these calls are now (correctly) identified as erroneous. My guess is that they only worked in the past because of problems with our lookups. * chore: format code * feat!: support defining types in modules This commit adds support for defining types (using deftype) in modules. Previously, all types were hoisted to the top level of the type environment. After this commit, the type environment supports defining nested modules just like the value env, so, calling the following: ``` (defmodule Foo (deftype Bar Baz)) ``` Adds the following to the type env: ``` Foo : Module = { Bar : Type } ``` and the following to the value env: ``` Foo : Module = { Bar : Module = { Baz : (Fn [] Foo.Bar) copy : (Fn [(Ref Foo.Bar q)] Foo.Bar) delete : (Fn [Foo.Bar] ()) get-tag : (Fn [(Ref Foo.Bar q)] Int) prn : (Fn [(Ref Foo.Bar q)] String) str : (Fn [(Ref Foo.Bar q)] String) } } ``` Such a type is *distinct* from any type defined at the top level that happens to also have the name `Bar`. This commit also updates info and tests to account for types in modules. BREAKING CHANGE: This change is breaking since it alters the names of types that were previously defined in modules. A good example of this is the `Id` type in the `Color` module. Previously, one could refer to this type by simply typing `Id` since it was hoisted to the top level. Now it *must* be referred to by `Color.Id` since `Id` at the top level of the type env and `Color.Id` (Id in the color module) are considered to be distinct types. * chore: format code * refactor: use concat instead of intercalate * chore: remove excess parentheses * chore: Add todo to return IO () in printIfFound
2020-12-22 15:27:57 +03:00
(Set.length &(the (Set Int) (Set.create)))
2018-11-07 18:11:38 +03:00
"length works on empty map"
)
(assert-equal test
false
Support defining types in modules (BREAKING) (#1084) * fix: don't set the inner env to globals in type mods Previously, we set the inner environment of a type generated module to the global env in cases where the overarching context didn't have an inner env. This leads to problems where by the recognition of modules is inconsistent, and one can't use the names of types as submodules in certain circumstances. This commit fixes that issue. * refactor: refactor primitiveDefmodule This refactor fixes a issues with meta information on submodules, for instance, sigs on submodule functions used to result in a compiler error about ambiguous identifiers. This fixes that. Unfortunately, I don't have a precise idea about what exactly was wrong with the original definition of this function. My suspicion is that the recursion originally altered submodule paths in the wrong way, but I'm not certain. In any case it's fixed. * fix: ensure macros are expanded in the correct module Previously, macro expansions folded over all forms after the top level form, without performing any context updates on encountered `defmodules`. This created an issue in which macro calls that produced new bindings, "meta stubs", were *hoisted* out of submodules and into the top-level module, creating duplicate definitions. This commit fixes that issue by adding a special case for defmodule in macroExpand. * fix: ensure submodules and globals don't conflict Previously, our module lookups during new module definition always eventually fell back to the global environment, which caused submodules that happen to share a name with a global module to be confused with the global module. This change fixes that, so now one can define both `Dynamic` (global) and `Foo.Dynamic` without issue. * fix: remove old prefixes from vector tests Commit 7b7cb5d1e replaced /= with a generic function. However, the vector tests still called the specific Vector variants of this function, which were removed when the generic was introduced. After recent changes, these calls are now (correctly) identified as erroneous. My guess is that they only worked in the past because of problems with our lookups. * chore: format code * feat!: support defining types in modules This commit adds support for defining types (using deftype) in modules. Previously, all types were hoisted to the top level of the type environment. After this commit, the type environment supports defining nested modules just like the value env, so, calling the following: ``` (defmodule Foo (deftype Bar Baz)) ``` Adds the following to the type env: ``` Foo : Module = { Bar : Type } ``` and the following to the value env: ``` Foo : Module = { Bar : Module = { Baz : (Fn [] Foo.Bar) copy : (Fn [(Ref Foo.Bar q)] Foo.Bar) delete : (Fn [Foo.Bar] ()) get-tag : (Fn [(Ref Foo.Bar q)] Int) prn : (Fn [(Ref Foo.Bar q)] String) str : (Fn [(Ref Foo.Bar q)] String) } } ``` Such a type is *distinct* from any type defined at the top level that happens to also have the name `Bar`. This commit also updates info and tests to account for types in modules. BREAKING CHANGE: This change is breaking since it alters the names of types that were previously defined in modules. A good example of this is the `Id` type in the `Color` module. Previously, one could refer to this type by simply typing `Id` since it was hoisted to the top level. Now it *must* be referred to by `Color.Id` since `Id` at the top level of the type env and `Color.Id` (Id in the color module) are considered to be distinct types. * chore: format code * refactor: use concat instead of intercalate * chore: remove excess parentheses * chore: Add todo to return IO () in printIfFound
2020-12-22 15:27:57 +03:00
(Set.contains? &(the (Set String) (Set.create)) "1")
2018-11-07 18:11:38 +03:00
"contains? works on empty map"
)
(assert-equal test
true
(Set.contains? &(Set.put (Set.create) "1") "1")
"contains? works"
)
(assert-equal test
true
(Set.contains? &(Set.put (Set.create) &-7) &-7)
"contains? works with negative keys"
)
2018-11-07 18:11:38 +03:00
(assert-equal test
true
(Set.empty? &(the (Set Int) (Set.create)))
"empty? works on empty map"
)
(assert-equal test
false
(Set.empty? &(Set.put (Set.create) "1"))
"empty? works"
)
(assert-equal test
true
(Set.empty? &(Set.remove (Set.put (Set.create) "1") "1"))
"remove works"
)
2018-12-18 16:58:58 +03:00
(assert-equal test
true
(Set.all? &(fn [i] (Int.even? @i)) &(Set.from-array &[2 4 6]))
"Set.all? works I"
)
(assert-equal test
false
(Set.all? &(fn [i] (Int.even? @i)) &(Set.from-array &[2 4 7]))
"Set.all? works II"
)
(assert-equal test
true
(Set.all? &(fn [i] false) &(the (Set Int) (Set.create)))
"Set.all? works on empty set"
)
(assert-equal test
true
(Set.subset? &(Set.from-array &[1 2]) &(Set.from-array &[1 2 3]))
"subset? works"
)
(assert-equal test
false
(Set.subset? &(Set.from-array &[1 2 3]) &(Set.from-array &[1 2]))
"subset? works II"
)
(assert-equal test
true
(Set.= &(Set.from-array &[1 3 5]) &(Set.from-array &[1 3 5]))
"Set.= works"
)
(assert-equal test
false
(Set.= &(Set.from-array &[1 3]) &(Set.from-array &[1 3 5]))
"Set.= works II"
)
(assert-equal test
false
(Set.= &(Set.from-array &[1 3 5]) &(Set.from-array &[1 3]))
"Set.= works III"
)
(assert-equal test
false
(Set.= &(Set.from-array &[1 3 5]) &(Set.from-array &[1 4 5]))
"Set.= works IV"
)
2018-12-07 14:15:23 +03:00
(assert-equal test
61
(Set.reduce &(fn [state i] (+ state (* 10 @i)))
2018-12-07 14:15:23 +03:00
1
&(Set.from-array &[1 2 3]))
"reduce works"
)
(assert-equal test
&(Set.from-array &[3 5])
&(Set.intersection &(Set.from-array &[1 3 5]) &(Set.from-array &[3 5 7]))
"intersection works"
)
(assert-equal test
&(Set.from-array &[1 3 5 7])
&(Set.union &(Set.from-array &[1 3 5]) &(Set.from-array &[3 5 7]))
"union works"
)
(assert-equal test
&(Set.from-array &[1])
&(Set.difference &(Set.from-array &[1 3 5]) &(Set.from-array &[3 5 7]))
"difference works"
)
2018-11-07 18:11:38 +03:00
(assert-equal test
"{ @\"hi\" @\"bye\" }"
&(str &(Set.from-array &[@"hi" @"bye"]))
"stringification works"
)
2018-12-12 09:21:16 +03:00
(assert-equal test
"{ 2 }"
&(str &(Set.from-array &[2]))
"stringification with ints"
)
(assert-equal test
&[1]
&(Set.to-array &(Set.put (Set.create) &1))
"Set.to-array works 1"
)
(assert-equal test
2
(Array.length &(Set.to-array &(Set.from-array &[1 2])))
"Set.to-array works 2"
2018-11-07 18:11:38 +03:00
))