-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTests.hs
223 lines (201 loc) · 5.57 KB
/
Tests.hs
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
module Doctest where
import CoreLanguage -- Allows writing programs
import Interpreter -- Allows running programs `compile <name>`
import Prelude hiding ( and
, or
, subtract
)
-- | Testing the `or` syntactic sugar
--
-- >>> testOrFalse
-- Result (Boolean False)
--
testOrFalse :: MaybeError VarVal
testOrFalse =
run [Return (or (Literal (Boolean False)) (Literal (Boolean False)))]
-- |
-- >>> testOrTrue
-- Result (Boolean True)
--
testOrTrue :: MaybeError VarVal
testOrTrue =
run [Return (or (Literal (Boolean False)) (Literal (Boolean True)))]
-- | Testing the list assignment
--
-- >>> testListAssignment
-- Result (IntList [1,2,3])
--
testListAssignment :: MaybeError VarVal
testListAssignment =
run [Set "myList" (Literal (IntList [1, 2, 3])), Return (Variable "myList")]
-- | Testing the list insertion
--
-- >>> testListInsertion
-- Result (IntList [0,1,2,3])
--
testListInsertion :: MaybeError VarVal
testListInsertion = run
[ Set "myList" (Literal (IntList [1, 2, 3]))
, Insert "myList" (Literal (Int 0)) (Literal (Int 0))
, Return (Variable "myList")
]
-- | Testing the list accessing
--
-- >>> testListAccess
-- Result (Int 2)
--
testListAccess :: MaybeError VarVal
testListAccess = run
[ Set "myList" (Literal (IntList [1, 2, 3]))
, Return (Element (Variable "myList") (Literal (Int 1)))
]
-- | Testing the list deletion
--
-- >>> testListDeletion
-- Result (IntList [0,1,3])
--
testListDeletion :: MaybeError VarVal
testListDeletion = run
[ Set "myList" (Literal (IntList [0, 1, 2, 3]))
, Delete "myList" (Literal (Int 2))
, Return (Variable "myList")
]
-- | Testing the list index assignment
--
-- >>> testListIndexing
-- Result (IntList [0,8,2,3])
--
testListIndexing :: MaybeError VarVal
testListIndexing = run
[ Set "myList" (Literal (IntList [0, 1, 2, 3]))
, assign "myList" (Literal (Int 1)) (Literal (Int 8))
, Return (Variable "myList")
]
-- | Testing list length
--
-- >>> testListLength
-- Result (Int 4)
--
testListLength :: MaybeError VarVal
testListLength = run
[ Set "myList" (Literal (IntList [0, 1, 2, 3]))
, Return (Length (Variable "myList"))
]
-- | Testing built-in append
--
-- >>> testListLength
-- Result (Int 4)
--
testListAppend :: MaybeError VarVal
testListAppend = run
[ Set "myList" (Literal (IntList [0, 1, 2, 3]))
, Return (Function "append" [Variable "myList", Literal (Int 4)])
]
-- | Testing built-in range
--
-- >>> testRange
-- Result (IntList [0,1,2,3])
--
testRange :: MaybeError VarVal
testRange = run [Return (Function "range" [Literal (Int 4)])]
-- | Testing the string assignment
--
-- >>> testStringAssignment
-- Result (String "hello")
--
testStringAssignment :: MaybeError VarVal
testStringAssignment = run
[ Set "myString" (Literal (String "hello"))
, Set "otherString" (Variable "myString")
, Return (Variable "myString")
]
-- | Testing the string insertion
--
-- >>> testStringInsertion
-- Result (String "Hello, world")
--
testStringInsertion :: MaybeError VarVal
testStringInsertion = run
[ Set "myString" (Literal (String "ello, world"))
, Insert "myString" (Literal (Int 0)) (Literal (Character 'H'))
, Return (Variable "myString")
]
-- | Testing the string accessing
--
-- >>> testStringAccess
-- Result (Character 'e')
--
testStringAccess :: MaybeError VarVal
testStringAccess = run
[ Set "myString" (Literal (String "Hello, world"))
, Return (Element (Variable "myString") (Literal (Int 1)))
]
-- | Testing the string deletion
--
-- >>> testStringDeletion
-- Result (String "Hello, world")
--
testStringDeletion :: MaybeError VarVal
testStringDeletion = run
[ Set "myString" (Literal (String "Hello, woorld"))
, Delete "myString" (Literal (Int 8))
, Return (Variable "myString")
]
-- | Testing the list index assignment
--
-- >>> testStringIndexing
-- Result (String "Hello, world")
--
testStringIndexing :: MaybeError VarVal
testStringIndexing = run
[ Set "myString" (Literal (String "Hello, w0rld"))
, assign "myString" (Literal (Int 8)) (Literal (Character 'o'))
, Return (Variable "myString")
]
-- | Testing list length
--
-- >>> testStringLength
-- Result (Int 12)
--
testStringLength :: MaybeError VarVal
testStringLength = run
[ Set "myString" (Literal (String "Hello, world"))
, Return (Length (Variable "myString"))
]
-- | Testing built-in append
--
-- >>> testStringAppend
-- Result (String "Hello, world!")
--
testStringAppend :: MaybeError VarVal
testStringAppend = run
[ Set "myString" (Literal (String "Hello, world"))
, Return (Function "append" [Variable "myString", Literal (Character '!')])
]
-- | Testing string concatenation
--
-- >>> testStringConcat
-- Result (String "You're a wizard, Harry")
--
testStringConcat :: MaybeError VarVal
testStringConcat = run
[ Set "myString" (Literal (String "You're a wizard, "))
, Return (Concat (Variable "myString") (Literal (String "Harry")))
]
-- | Tests casting string to int
--
-- >>> testCasting
-- Result (Int 12382)
--
testCasting :: MaybeError VarVal
testCasting = run
[ Set "myString" (Cast (Literal (String "12382")) TInt)
, Return (Variable "myString")
]
-- | Tests casting string to int
--
-- >>> testCastingFlt
-- Result (Float 69.42)
--
testCastingFlt :: MaybeError VarVal
testCastingFlt = run [Return (Cast (Literal (String "69.420")) TFlt)]