Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove duplication and cleanup the code

  • Loading branch information...
commit 8b9413ba77de2f4a098843d9b65dc1983ef6dd1d 1 parent 9be17e3
@alco authored
View
106 lib/dict/dict.ex
@@ -1,26 +1,24 @@
-defrecord Dict.Record, d: nil
-
-defimpl GenDict, for: Dict.Record do
+defimpl PDict, for: Tuple do
def keys(dict) do
- :dict.fetch_keys dict.d
+ :dict.fetch_keys dict
end
def values(dict) do
:dict.fold fn(_key, value, acc) ->
[value|acc]
- end, [], dict.d
+ end, [], dict
end
def size(dict) do
- :dict.size dict.d
+ :dict.size dict
end
def has_key?(dict, key) do
- :dict.is_key key, dict.d
+ :dict.is_key key, dict
end
def get(dict, key, default // nil) do
- case :dict.find(key, dict.d) do
+ case :dict.find(key, dict) do
match: {:ok, value}
value
match: :error
@@ -29,109 +27,45 @@ defimpl GenDict, for: Dict.Record do
end
def put(dict, key, value) do
- dict.update_d(:dict.store key, value, &1)
+ :dict.store key, value, dict
end
def put(dict, {key, value}) do
- dict.update_d(:dict.store key, value, &1)
+ :dict.store key, value, dict
end
def delete(dict, key) do
- dict.update_d(:dict.erase key, &1)
+ :dict.erase key, dict
end
def merge(d1, d2) do
- d1.update_d(:dict.merge fn(_k, _v1, v2) -> v2 end, &1, d2.d)
+ :dict.merge fn(_k, _v1, v2) -> v2 end, d1, d2
end
def merge(d1, d2, fun) do
- d1.update_d(:dict.merge fun, &1, d2.d)
- end
-
- def extend(dict, pairs) when is_list(pairs) do
- Enum.reduce pairs, dict, fn(pair, dict) ->
- put(dict, pair)
- end
- end
-
- def extend(dict, pairs, transform) when is_list(pairs) and is_function(transform) do
- Enum.reduce pairs, dict, fn(i, dict) ->
- pair = transform.(i)
- put(dict, pair)
- end
- end
-
- def extend(dict, keys, values) when is_list(keys) and is_list(values) do
- if :erlang.length(keys) !== :erlang.length(values) do
- raise ArgumentError, "Both arguments must have equal size"
- else:
- extend(dict, List.zip(keys, values))
- end
+ :dict.merge fun, d1, d2
end
def update(dict, key, fun) do
- dict.update_d(:dict.update key, fun, &1)
+ :dict.update key, fun, dict
end
def update(dict, key, initial, fun) do
- dict.update_d(:dict.update key, fun, initial, &1)
+ :dict.update key, fun, initial, dict
+ end
+
+ def empty(_) do
+ :dict.new
end
end
defmodule Dict do
+ use Dict.Common
+
@doc """
Creates a new empty Dict.
"""
def new do
- Dict.Record.new [d: :dict.new]
- end
-
- @doc """
- Creates a new Dict with one entry.
- """
- def new({key, value}) do
- GenDict.put new(), {key, value}
- end
-
- @doc """
- Creates a new Dict from a list of pairs.
-
- ## Examples
-
- Dict.new [{:b,1},{:a,2}]
- #=> [a: 1, b: 2]
-
- """
- def new(pairs) when is_list(pairs) do
- GenDict.extend new(), pairs
- end
-
- @doc """
- Creates a new Dict from a list of elements with the
- help of the transformation function.
-
- ## Examples
-
- Dict.new ["a", "b"], fn(x) -> {x, x} end
- #=> ["a": "a", "b": "b"]
- """
- def new(list, transform) when is_list(list) and is_function(transform) do
- GenDict.extend new(), list, transform
- end
-
- @doc """
- Creates a new Dict with one entry for each element in `keys` and a
- corresponding element in `values`. Raises an error if `keys` and `values`
- have different size.
- """
- def new(keys, values) when is_list(keys) and is_list(values) do
- GenDict.extend new(), keys, values
- end
-
- @doc """
- Creates a new Dict from Erlang's dict.
- """
- def from_dict(erlang_dict) do
- Dict.Record.new [d: erlang_dict]
+ :dict.new
end
end
View
137 lib/dict/gen_dict.ex
@@ -1,4 +1,4 @@
-defprotocol GenDict, [
+defprotocol PDict, [
# @doc """
# Returns a list containing all dict's keys.
#
@@ -7,7 +7,7 @@ defprotocol GenDict, [
#
# ## Examples
#
-# GenDict.keys [a: 1, b: 2] #=> [:a,:b]
+# PDict.keys [a: 1, b: 2] #=> [:a,:b]
#
# """
keys(dict),
@@ -17,7 +17,7 @@ defprotocol GenDict, [
#
# ## Examples
#
-# GenDict.values [a: 1, b: 2] #=> [1,2]
+# PDict.values [a: 1, b: 2] #=> [1,2]
#
# """
values(dict),
@@ -27,7 +27,7 @@ defprotocol GenDict, [
#
# ## Examples
#
-# GenDict.size [a: 1, b: 2] #=> 2
+# PDict.size [a: 1, b: 2] #=> 2
#
# """
size(dict),
@@ -37,8 +37,8 @@ defprotocol GenDict, [
#
# ## Examples
#
-# GenDict.has_key?([a:, 1], :a) #=> true
-# GenDict.has_key?([a:, 1], :b) #=> false
+# PDict.has_key?([a:, 1], :a) #=> true
+# PDict.has_key?([a:, 1], :b) #=> false
# """
has_key?(dict, key),
@@ -48,10 +48,11 @@ defprotocol GenDict, [
#
# ## Examples
#
-# GenDict.get [a: 1], :a #=> 1
-# GenDict.get [a: 1], :b #=> nil
-# GenDict.get [a: 1], :b, 3 #=> 3
+# PDict.get [a: 1], :a #=> 1
+# PDict.get [a: 1], :b #=> nil
+# PDict.get [a: 1], :b, 3 #=> 3
# """
+ get(dict, key),
get(dict, key, default),
# @doc """
@@ -60,9 +61,9 @@ defprotocol GenDict, [
#
# ## Examples
#
-# GenDict.put [a: 1, b: 2], :a, 3
+# PDict.put [a: 1, b: 2], :a, 3
# #=> [a: 3, b: 2]
-# GenDict.put [a: 1, b: 2], {:c, 3}
+# PDict.put [a: 1, b: 2], {:c, 3}
# #=> [a: 1, b: 2, c: 3]
# """
put(dict, key, val),
@@ -74,8 +75,8 @@ defprotocol GenDict, [
#
# ## Examples
#
-# GenDict.delete [a: 1, b: 2], :a #=> [b: 2]
-# GenDict.delete [b: 2], :a #=> [b: 2]
+# PDict.delete [a: 1, b: 2], :a #=> [b: 2]
+# PDict.delete [b: 2], :a #=> [b: 2]
# """
delete(dict, key),
@@ -85,7 +86,7 @@ defprotocol GenDict, [
#
# ## Examples
#
-# GenDict.merge [a: 1, b: 2], [a: 3, d: 4]
+# PDict.merge [a: 1, b: 2], [a: 3, d: 4]
# #=> [a:3, b:2, d: 4]
# """
merge(dict1, dict2),
@@ -96,7 +97,7 @@ defprotocol GenDict, [
#
# ## Examples
#
-# GenDict.merge [a: 1, b: 2], [a: 3, d: 4], fn(_k, v1, v2) ->
+# PDict.merge [a: 1, b: 2], [a: 3, d: 4], fn(_k, v1, v2) ->
# v1 + v2
# end
# #=> [a: 4, b: 2, d: 4]
@@ -104,49 +105,12 @@ defprotocol GenDict, [
merge(dict1, dict2, fun),
# @doc """
-# Extends the dict with entries from the list of pairs `pairs`. Values for
-# existing keys are replaced by the new values from the `pairs` list.
-#
-# ## Examples
-#
-# GenDict.extend [a: 1, b: 2], [{:a, 3}, {:c, 4}]
-# #=> [a: 3, b: 2, c: 4]
-#
-# """
- extend(dict, pairs),
-
-# @doc """
-# Extends the dict with entries obtained by applying the transformation
-# function `transform` to each element in `list`. The overwrite semantics is
-# similar to extend/2.
-#
-# ## Examples
-#
-# GenDict.extend [a: 1, b: 2], [:a, :c], fn(x) -> {x, x} end
-# #=> [a: :a, b: 1, c: :c]
-#
-# """
- extend(dict, list, transform),
-
-# @doc """
-# Extends the dict with entries formed by corresponding elements from `keys`
-# and `values`. Raises an error if `keys` and `values` have different size.
-#
-# ## Examples
-#
-# GenDict.extend [a: 1, b: 2], [:b, :c], [3, 4]
-# #=> [a: 1, b: 3, c: 4]
-#
-# """
- extend(dict, keys, values),
-
-# @doc """
# Update a value in `dict` by calling `fun` on the value to get a new
# value. An exception is generated if `key` is not present in the dict.
#
# ## Examples
#
-# GenDict.update [a: 1, b: 2], :a, fn(val) -> -val end
+# PDict.update [a: 1, b: 2], :a, fn(val) -> -val end
# #=> [a: -1, b: 2]
#
# """
@@ -159,9 +123,72 @@ defprotocol GenDict, [
#
# ## Examples
#
-# GenDict.update [a: 1, b: 2], :c, 3, fn(val) -> -val end
+# PDict.update [a: 1, b: 2], :c, 3, fn(val) -> -val end
# #=> [a: 1, b: 2, c: 3]
#
# """
- update(dict, key, initial, fun)
+ update(dict, key, initial, fun),
+
+# @doc """
+# Returns an empty dict of the same type as `dict`.
+# """
+ empty(dict)
], only: [Record, List]
+
+
+defmodule Dict.Common do
+ defmacro __using__(module, _opts // []) do
+ quote do
+ @doc """
+ Creates a new dict with one entry.
+ """
+ def new({key, value}) do
+ PDict.put unquote(module).new(), {key, value}
+ end
+
+ @doc """
+ Creates a new dict from a list of pairs.
+
+ ## Examples
+
+ Dict.new [{:b,1},{:a,2}]
+ #=> [a: 1, b: 2]
+
+ """
+ def new(pairs) when is_list(pairs) do
+ Enum.reduce pairs, unquote(module).new(), fn(pair, dict) ->
+ PDict.put(dict, pair)
+ end
+ end
+
+ @doc """
+ Creates a new dict from a list of elements with the
+ help of the transformation function.
+
+ ## Examples
+
+ Dict.new ["a", "b"], fn(x) -> {x, x} end
+ #=> ["a": "a", "b": "b"]
+ """
+ def new(list, transform) when is_list(list) and is_function(transform) do
+ Enum.reduce list, unquote(module).new(), fn(i, dict) ->
+ pair = transform.(i)
+ PDict.put(dict, pair)
+ end
+ end
+
+ @doc """
+ Creates a new dict with one entry for each element in `keys` and a
+ corresponding element in `values`. Raises an error if `keys` and `values`
+ have different size.
+ """
+ def new(keys, values) when is_list(keys) and is_list(values) do
+ if :erlang.length(keys) !== :erlang.length(values) do
+ raise ArgumentError, message: "Both arguments must have equal size"
+ else:
+ unquote(module).new List.zip(keys, values)
+ end
+ end
+ end
+ end
+end
View
71 lib/dict/orddict.ex
@@ -1,5 +1,5 @@
# We're assuming that the List contains sorted pairs of key-value entries
-defimpl GenDict, for: List do
+defimpl PDict, for: List do
def keys(dict) do
:orddict.fetch_keys dict
end
@@ -45,27 +45,6 @@ defimpl GenDict, for: List do
:orddict.merge fun, d1, d2
end
- def extend(dict, pairs) when is_list(pairs) do
- Enum.reduce pairs, dict, fn(pair, dict) ->
- put(dict, pair)
- end
- end
-
- def extend(dict, pairs, transform) when is_list(pairs) and is_function(transform) do
- Enum.reduce pairs, dict, fn(i, dict) ->
- pair = transform.(i)
- put(dict, pair)
- end
- end
-
- def extend(dict, keys, values) when is_list(keys) and is_list(values) do
- if :erlang.length(keys) !== :erlang.length(values) do
- raise ArgumentError, "Both arguments must have equal size"
- else:
- extend(dict, List.zip(keys, values))
- end
- end
-
def update(dict, key, fun) do
:orddict.update key, fun, dict
end
@@ -73,55 +52,19 @@ defimpl GenDict, for: List do
def update(dict, key, initial, fun) do
:orddict.update key, fun, initial, dict
end
+
+ def empty(_) do
+ []
+ end
end
defmodule Orddict do
+ use Dict.Common
+
@doc """
Creates a new empty Orddict.
"""
def new do
[]
end
-
- @doc """
- Creates a new Orddict with one entry.
- """
- def new({key, value}) do
- GenDict.put new(), {key, value}
- end
-
- @doc """
- Creates a new Orddict from a list of pairs.
-
- ## Examples
-
- Dict.new [{:b,1},{:a,2}]
- #=> [a: 1, b: 2]
-
- """
- def new(pairs) when is_list(pairs) do
- GenDict.extend new(), pairs
- end
-
- @doc """
- Creates a new Orddict from a list of elements with the
- help of the transformation function.
-
- ## Examples
-
- Dict.new ["a", "b"], fn(x) -> {x, x} end
- #=> ["a": "a", "b": "b"]
- """
- def new(list, transform) when is_list(list) and is_function(transform) do
- GenDict.extend new(), list, transform
- end
-
- @doc """
- Creates a new Orddict with one entry for each element in `keys` and a
- corresponding element in `values`. Raises an error if `keys` and `values`
- have different size.
- """
- def new(keys, values) when is_list(keys) and is_list(values) do
- GenDict.extend new(), keys, values
- end
end
View
217 test/elixir/dict_test.exs
@@ -1,109 +1,122 @@
Code.require_file "../test_helper", __FILE__
-defmodule DictTest do
- use ExUnit.Case
-
- test :new_pair do
- dict = Dict.new {"a", 0}
- assert is_record(dict, Dict.Record)
- assert_equal 1, GenDict.size dict
-
- assert GenDict.has_key? dict, "a"
- assert_equal 0, GenDict.get dict, "a"
- end
-
- test :new_pairs do
- dict = Dict.new [{"first key", 1}, {"second key", 2}]
- assert is_record(dict, Dict.Record)
- assert_equal 2, GenDict.size dict
-
- assert_equal ["first key", "second key"], List.sort GenDict.keys dict
- assert_equal [1, 2], List.sort GenDict.values dict
- end
-
- test :new_two_lists do
- dict = Dict.new ["first key", "second key"], [1, 2]
- assert_equal 2, GenDict.size dict
- assert_equal 1, GenDict.get dict, "first key"
- assert_equal 2, GenDict.get dict, "second key"
-
- assert_raises ArgumentError, Dict.new ["first key"], [1, 2]
- end
-
- test :new_pairs_with_transform do
- dict = Dict.new [{1}, {2}, {3}], fn({x}) -> { {x}, x } end
- assert is_record(dict, Dict.Record)
- assert_equal 3, GenDict.size dict
-
- assert_equal [{1}, {2}, {3}], List.sort GenDict.keys dict
- assert_equal [1, 2, 3], List.sort GenDict.values dict
- end
-
- test :get do
- assert_equal 1, GenDict.get(create_dict, "first_key")
- assert_equal 2, GenDict.get(create_dict, "second_key")
- assert_equal nil, GenDict.get(create_dict, "other_key")
- assert_equal "default", GenDict.get(create_empty_dict, "first_key", "default")
- end
-
- test :keys do
- assert_equal ["first_key", "second_key"], List.sort GenDict.keys create_dict
- assert_equal [], GenDict.keys create_empty_dict
- end
-
- test :values do
- assert_equal [1, 2], List.sort GenDict.values(create_dict)
- assert_equal [], GenDict.values create_empty_dict
- end
-
- test :delete do
- mdict = GenDict.delete create_dict, "second_key"
- assert_equal 1, GenDict.size mdict
- assert GenDict.has_key? mdict, "first_key"
- refute GenDict.has_key? mdict, "second_key"
-
- mdict = GenDict.delete(create_dict, "other_key")
- assert_equal mdict, create_dict
- assert_equal 0, GenDict.size GenDict.delete(create_empty_dict, "other_key")
- end
-
- test :put do
- dict = GenDict.put(create_empty_dict, {"first_key", 1})
- assert_equal 1, GenDict.get dict, "first_key"
-
- dict = GenDict.put(create_dict, {"first_key", {1}})
- assert_equal {1}, GenDict.get "first_key"
- assert_equal 2, GenDict.get "second_key"
- end
-
- test :merge do
- empty_dict = create_empty_dict
- dict = create_dict
- assert_equal dict, GenDict.merge empty_dict, dict
- assert_equal dict, GenDict.merge dict, empty_dict
- assert_equal dict, GenDict.merge dict, dict
- assert_equal [], GenDict.merge empty_dict, empty_dict
-
- dict1 = Dict.new ["a", "b", "c"], [1, 2, 3]
- dict2 = Dict.new ["a", "c", "d"], [3, :a, 0]
- assert_equal Dict.new(["a", "b", "c", "d"], [3, 2, :a, 0]), GenDict.merge(dict1, dict2)
- end
-
- test :merge_with_function do
- dict1 = Dict.new ["a", "b"], [1, 2]
- dict2 = Dict.new ["a", "d"], [3, 4]
- result = GenDict.merge dict1, dict2, fn(_k, v1, v2) ->
- v1 + v2
+defmodule DictTest.Common do
+ defmacro __using__(module, _opts // []) do
+ quote do
+ use ExUnit.Case
+
+ test :new_pair do
+ dict = new_dict {"a", 0}
+ assert_equal 1, PDict.size dict
+
+ assert PDict.has_key? dict, "a"
+ assert_equal 0, PDict.get dict, "a"
+ end
+
+ test :new_pairs do
+ dict = new_dict [{"first key", 1}, {"second key", 2}]
+ assert_equal 2, PDict.size dict
+
+ assert_equal ["first key", "second key"], List.sort PDict.keys dict
+ assert_equal [1, 2], List.sort PDict.values dict
+ end
+
+ test :new_two_lists do
+ dict = new_dict ["first key", "second key"], [1, 2]
+ assert_equal 2, PDict.size dict
+ assert_equal 1, PDict.get dict, "first key"
+ assert_equal 2, PDict.get dict, "second key"
+
+ assert_raises ArgumentError, new_dict(["first key"], [1, 2])
+ end
+
+ test :new_pairs_with_transform do
+ dict = new_dict [{1}, {2}, {3}], fn({x}) -> { {x}, x } end
+ assert_equal 3, PDict.size dict
+
+ assert_equal [{1}, {2}, {3}], List.sort PDict.keys dict
+ assert_equal [1, 2, 3], List.sort PDict.values dict
+ end
+
+ test :get do
+ assert_equal 1, PDict.get(new_dict, "first_key")
+ assert_equal 2, PDict.get(new_dict, "second_key")
+ assert_equal nil, PDict.get(new_dict, "other_key")
+ assert_equal "default", PDict.get(empty_dict, "first_key", "default")
+ end
+
+ test :keys do
+ assert_equal ["first_key", "second_key"], List.sort PDict.keys new_dict
+ assert_equal [], PDict.keys empty_dict
+ end
+
+ test :values do
+ assert_equal [1, 2], List.sort PDict.values(new_dict)
+ assert_equal [], PDict.values empty_dict
+ end
+
+ test :delete do
+ mdict = PDict.delete new_dict, "second_key"
+ assert_equal 1, PDict.size mdict
+ assert PDict.has_key? mdict, "first_key"
+ refute PDict.has_key? mdict, "second_key"
+
+ mdict = PDict.delete(new_dict, "other_key")
+ assert_equal mdict, new_dict
+ assert_equal 0, PDict.size PDict.delete(empty_dict, "other_key")
+ end
+
+ test :put do
+ dict = PDict.put(empty_dict, {"first_key", 1})
+ assert_equal 1, PDict.get dict, "first_key"
+
+ dict = PDict.put(new_dict, {"first_key", {1}})
+ assert_equal {1}, PDict.get dict, "first_key"
+ assert_equal 2, PDict.get dict, "second_key"
+ end
+
+ test :merge do
+ dict = new_dict
+ assert_equal dict, PDict.merge empty_dict, dict
+ assert_equal dict, PDict.merge dict, empty_dict
+ assert_equal dict, PDict.merge dict, dict
+ assert_equal empty_dict, PDict.merge empty_dict, empty_dict
+
+ dict1 = new_dict ["a", "b", "c"], [1, 2, 3]
+ dict2 = new_dict ["a", "c", "d"], [3, :a, 0]
+ assert_equal new_dict(["a", "b", "c", "d"], [3, 2, :a, 0]), PDict.merge(dict1, dict2)
+ end
+
+ test :merge_with_function do
+ dict1 = new_dict ["a", "b"], [1, 2]
+ dict2 = new_dict ["a", "d"], [3, 4]
+ result = PDict.merge dict1, dict2, fn(_k, v1, v2) ->
+ v1 + v2
+ end
+ assert_equal new_dict(["a", "b", "d"], [4, 2, 4]), result
+ end
+
+ test :has_key do
+ dict = new_dict [{"a", 1}]
+ assert PDict.has_key?(dict, "a")
+ refute PDict.has_key?(dict, "b")
+ end
+
+ defp empty_dict, do: unquote(module).new
+ defp new_dict({k, v}), do: unquote(module).new {k, v}
+ defp new_dict(list // [{"first_key", 1}, {"second_key", 2}]), do: unquote(module).new list
+ defp new_dict(list, transform) when is_function(transform), do: unquote(module).new list, transform
+ defp new_dict(keys, values), do: unquote(module).new keys, values
end
- assert_equal Dict.new(["a", "b", "d"], [4, 2, 4]), result
end
+end
- test :has_key do
- dict = Dict.new [{"a", 1}]
- assert GenDict.has_key?(dict, "a")
- refute GenDict.has_key?(dict, "b")
- end
+defmodule DictTest do
+ require DictTest.Common
+ DictTest.Common.__using__(Dict)
+end
- defp create_empty_dict, do: Dict.new
- defp create_dict(list // [{"first_key", 1}, {"second_key", 2}]), do: Dict.new list
+defmodule OrddictTest do
+ require DictTest.Common
+ DictTest.Common.__using__(Orddict)
end
View
109 test/elixir/orddict_test.exs
@@ -1,109 +0,0 @@
-Code.require_file "../test_helper", __FILE__
-
-defmodule OrddictTest do
- use ExUnit.Case
-
- test :new_pair do
- dict = Orddict.new {"a", 0}
- assert is_list(dict)
- assert_equal 1, GenDict.size dict
-
- assert GenDict.has_key? dict, "a"
- assert_equal 0, GenDict.get dict, "a"
- end
-
- test :new_pairs do
- dict = Orddict.new [{"first key", 1}, {"second key", 2}]
- assert is_list(dict)
- assert_equal 2, GenDict.size dict
-
- assert_equal ["first key", "second key"], List.sort GenDict.keys dict
- assert_equal [1, 2], List.sort GenDict.values dict
- end
-
- test :new_two_lists do
- dict = Orddict.new ["first key", "second key"], [1, 2]
- assert_equal 2, GenDict.size dict
- assert_equal 1, GenDict.get dict, "first key"
- assert_equal 2, GenDict.get dict, "second key"
-
- assert_raises ArgumentError, Dict.new ["first key"], [1, 2]
- end
-
- test :new_pairs_with_transform do
- dict = Orddict.new [{1}, {2}, {3}], fn({x}) -> { {x}, x } end
- assert is_list(dict)
- assert_equal 3, GenDict.size dict
-
- assert_equal [{1}, {2}, {3}], List.sort GenDict.keys dict
- assert_equal [1, 2, 3], List.sort GenDict.values dict
- end
-
- test :get do
- assert_equal 1, GenDict.get(create_dict, "first_key")
- assert_equal 2, GenDict.get(create_dict, "second_key")
- assert_equal nil, GenDict.get(create_dict, "other_key")
- assert_equal "default", GenDict.get(create_empty_dict, "first_key", "default")
- end
-
- test :keys do
- assert_equal ["first_key", "second_key"], List.sort GenDict.keys create_dict
- assert_equal [], GenDict.keys create_empty_dict
- end
-
- test :values do
- assert_equal [1, 2], List.sort GenDict.values(create_dict)
- assert_equal [], GenDict.values create_empty_dict
- end
-
- test :delete do
- mdict = GenDict.delete create_dict, "second_key"
- assert_equal 1, GenDict.size mdict
- assert GenDict.has_key? mdict, "first_key"
- refute GenDict.has_key? mdict, "second_key"
-
- mdict = GenDict.delete(create_dict, "other_key")
- assert_equal mdict, create_dict
- assert_equal 0, GenDict.size GenDict.delete(create_empty_dict, "other_key")
- end
-
- test :put do
- dict = GenDict.put(create_empty_dict, {"first_key", 1})
- assert_equal 1, GenDict.get dict, "first_key"
-
- dict = GenDict.put(create_dict, {"first_key", {1}})
- assert_equal {1}, GenDict.get "first_key"
- assert_equal 2, GenDict.get "second_key"
- end
-
- test :merge do
- empty_dict = create_empty_dict
- dict = create_dict
- assert_equal dict, GenDict.merge empty_dict, dict
- assert_equal dict, GenDict.merge dict, empty_dict
- assert_equal dict, GenDict.merge dict, dict
- assert_equal [], GenDict.merge empty_dict, empty_dict
-
- dict1 = Orddict.new ["a", "b", "c"], [1, 2, 3]
- dict2 = Orddict.new ["a", "c", "d"], [3, :a, 0]
- assert_equal Orddict.new(["a", "b", "c", "d"], [3, 2, :a, 0]), GenDict.merge(dict1, dict2)
- end
-
- test :merge_with_function do
- dict1 = Orddict.new ["a", "b"], [1, 2]
- dict2 = Orddict.new ["a", "d"], [3, 4]
- result = GenDict.merge dict1, dict2, fn(_k, v1, v2) ->
- v1 + v2
- end
- assert_equal Orddict.new(["a", "b", "d"], [4, 2, 4]), result
- end
-
- test :has_key do
- dict = Orddict.new [{"a", 1}]
- assert GenDict.has_key?(dict, "a")
- refute GenDict.has_key?(dict, "b")
- end
-
- defp create_empty_dict, do: Orddict.new
- defp create_dict(list // [{"first_key", 1}, {"second_key", 2}]), do: Orddict.new list
-end
Please sign in to comment.
Something went wrong with that request. Please try again.