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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
open OUnit2
module TestList = struct
let test_linearize ctx = begin
let input = [
['1'; '2'; '3'];
['A'; 'B'];
['W'; 'X'; 'Y'; 'Z'];
['O'];
] in
let expected = [
'1'; 'A'; 'W'; 'O'; 'X'; 'B'; '2'; '3'; 'Y'; 'Z'
] in
let result = Tools.List.linearize input in
let to_string elems = begin
let result_buffer = Buffer.create 16 in
List.iter (Buffer.add_char result_buffer) elems;
Buffer.contents result_buffer
end in
assert_equal
~msg:(Printf.sprintf "Expected %s but got %s" (to_string expected) (to_string result) )
expected
result
end
let tests = "list_test">::: [
"test_list_linearize" >:: test_linearize;
]
end
module TestString = struct
let _msg ~expected ~result =
Printf.sprintf "Expected %s but got %s"
expected
result
let test_string_of_ints ctx = begin
let result = Tools.String.string_of_ints 127 in
let expected = "\127" in
assert_equal
~msg:(_msg ~expected ~result)
expected
result
end
let test_string_of_ints_512 ctx = begin
let result = Tools.String.string_of_ints 512 in
let expected = "\002\000" in
assert_equal
~msg:(_msg ~expected ~result)
expected
result
end
let test_split ctx = begin
let result = Tools.String.split ~by:' ' "abc 123 456"
and expected = ("abc", "123 456") in
assert_equal expected result
end
let test_filter_float src expected ctx = begin
let result = Tools.String.filter_float src in
assert_equal
~msg:(_msg ~expected ~result)
expected
result
end
let tests = "string_test">::: [
"test_string_of_ints" >:: test_string_of_ints;
"test_string_of_ints_512" >:: test_string_of_ints_512;
"test_string_split" >:: test_split;
"filter_float" >:: test_filter_float "12." "12";
"filter_float2" >:: test_filter_float "12.5" "12.5";
"filter_float_empty" >:: test_filter_float "" "";
"filter_float_no_dot" >:: test_filter_float "123" "123";
]
end
module TestDate = struct
module Float = struct
type t = float
let add = ( +. )
let sub = ( -. )
let mult = ( *. )
let div = ( /. )
let floor = Pervasives.floor
let of_int = float_of_int
let to_int = int_of_float
let to_float x = x
end
module FDate = Date.Make(Float)
let epsilon = 1.0e-5
let (=.) a b = (abs_float (a-.b)) < epsilon
let test_get_julian_day ctx = begin
let result = FDate.get_julian_day 2016 01 01
and expected = 42370. in
let _msg ~expected ~result =
Printf.sprintf "Expected %f but got %f"
expected
result in
assert_equal
~cmp:(=.)
~msg:(_msg ~expected ~result)
expected
result
end
let test_from_julian_day ctx = begin
let _msg (y1, m1, d1) (y2, m2, d2) =
Printf.sprintf "Expected %d-%d-%d but got %d-%d-%d"
y1 m1 d1
y2 m2 d2
in
let result = FDate.date_from_julian_day 734.
and expected = (1902, 01, 03) in
assert_equal
~msg:(_msg expected result)
expected
result;
end
let test_parse_time ctx = begin
let result = FDate.from_string "1902-01-03T12:34:56"
and expected = 3966431. /. 5400.
(* =2415753.52425925925925925925 *)
and _msg ~expected ~result =
Printf.sprintf "Expected %f but got %f"
expected
result in
assert_equal
~cmp:(=.)
~msg:(_msg ~expected ~result)
expected
result
end
let test_time_from_julian_day ctx = begin
let _msg (h1, m1, s1) (h2, m2, s2) =
Printf.sprintf "Expected %d:%d:%f but got %d:%d:%f"
h1 m1 s1
h2 m2 s2
in
let result = FDate.time_from_julian_day @@ FDate.from_string "1902-01-03T12:34:56"
and expected = (12, 34, 56.) in
assert_equal
~msg:(_msg expected result)
~cmp:(fun (a, b, c) (a', b', c') -> a = a' && b = b' && c =. c')
expected
result
end
let test_time_add_hour ctx = begin
let (result:string) = FDate.from_string "1902-01-03T12:34:56"
|> fun x -> (x +. (1. /. 2.))
|> FDate.to_string in
let expected = "1902-01-04T00:34:56" in
let msg = Printf.sprintf "Expected %s but got %s" expected result in
assert_equal
~msg
expected
result
end
let test_time_add_hour2 ctx = begin
let (result:string) = FDate.from_string "1902-01-03T12:34:56"
|> fun x -> (x +. (3. /. 4.))
|> FDate.to_string in
let expected = "1902-01-04T00:34:56" in
let msg = Printf.sprintf "Expected %s but got %s" expected result in
assert_equal
~msg
expected
result
end
end
let tests = "tools_test">::: [
TestString.tests;
TestList.tests;
"test_get_julian_day" >:: TestDate.test_get_julian_day;
"test_from_julian_day" >:: TestDate.test_from_julian_day;
"test_parse_time" >:: TestDate.test_parse_time;
"test_time_from_julian_day" >:: TestDate.test_time_from_julian_day;
"test_time_add_hour" >:: TestDate.test_time_add_hour;
]
|