aboutsummaryrefslogtreecommitdiff
path: root/test/map_dict_test.gleam
blob: a0797c24de5bfa793a00aec75600367fa4aec824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import expect
import map_dict

pub fn from_list_test() {
  [
    {4, 0},
    {1, 0},
  ]
  |> map_dict:from_list
  |> map_dict:size
  |> expect:equal(_, 2)
}

pub fn has_key_test() {
  []
  |> map_dict:from_list
  |> map_dict:has_key(_, 1)
  |> expect:false

  [
      {1, 0},
  ]
  |> map_dict:from_list
  |> map_dict:has_key(_, 1)
  |> expect:true

  [
      {4, 0},
      {1, 0},
  ]
  |> map_dict:from_list
  |> map_dict:has_key(_, 1)
  |> expect:true

  [
    {4, 0},
    {1, 0},
  ]
  |> map_dict:from_list
  |> map_dict:has_key(_, 0)
  |> expect:false
}

pub fn new_test() {
  map_dict:new()
  |> map_dict:size
  |> expect:equal(_, 0)

  map_dict:new()
  |> map_dict:to_list
  |> expect:equal(_, [])
}

pub fn fetch_test() {
  let proplist = [
    {4, 0},
    {1, 1},
  ]
  let m = map_dict:from_list(proplist)

  m
  |> map_dict:fetch(_, 4)
  |> expect:equal(_, Ok(0))

  m
  |> map_dict:fetch(_, 1)
  |> expect:equal(_, Ok(1))

  m
  |> map_dict:fetch(_, 2)
  |> expect:is_error
}

pub fn put_test() {
  map_dict:new()
  |> map_dict:put(_, "a", 0)
  |> map_dict:put(_, "b", 1)
  |> map_dict:put(_, "c", 2)
  |> expect:equal(_, map_dict:from_list([{"a", 0}, {"b", 1}, {"c", 2}]))
}

pub fn map_values_test() {
  [
    {1, 0},
    {2, 1},
    {3, 2},
  ]
  |> map_dict:from_list
  |> map_dict:map_values(_, fn(k, v) { k + v })
  |> expect:equal(_, map_dict:from_list([{1, 1}, {2, 3}, {3, 5}]))
}

pub fn keys_test() {
  [
    {"a", 0},
    {"b", 1},
    {"c", 2},
  ]
  |> map_dict:from_list
  |> map_dict:keys
  |> expect:equal(_, ["a", "b", "c"])
}

pub fn values_test() {
  [
    {"a", 0},
    {"b", 1},
    {"c", 2},
  ]
  |> map_dict:from_list
  |> map_dict:values
  |> expect:equal(_, [0, 1, 2])
}

pub fn take_test() {
  [
    {"a", 0},
    {"b", 1},
    {"c", 2},
  ]
  |> map_dict:from_list
  |> map_dict:take(_, ["a", "b", "d"])
  |> expect:equal(_, map_dict:from_list([{"a", 0}, {"b", 1}]))
}

pub fn drop_test() {
  [
    {"a", 0},
    {"b", 1},
    {"c", 2},
  ]
  |> map_dict:from_list
  |> map_dict:drop(_, ["a", "b", "d"])
  |> expect:equal(_, map_dict:from_list([{"c", 2}]))
}