-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase32_unittest.opa
49 lines (38 loc) · 1.52 KB
/
base32_unittest.opa
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
/**
* Base32 unittest, based on the test vectors from rfc4648
*
* Running: make test
*
* @author Alok Menghrajani
*/
function expect_encode(Test.result res, string test_name, string s, string expected) {
Test.expect_equals(res, test_name, Base32.encode(binary_of_string(s)), expected)
}
function expect_decode(Test.result res, string test_name, string s, string expected) {
match(Base32.decode(s)) {
case {none}:
extra_message = "(expecting {expected}, got none)"
Test.fail(res, test_name, extra_message)
case {~some}:
Test.expect_equals(res, test_name, string_of_binary(some), expected)
}
}
function run_tests() {
t = Test.begin()
t = expect_encode(t, "empty string", "", "")
t = expect_decode(t, "empty string", "", "")
t = expect_encode(t, "one character", "f", "MY======")
t = expect_decode(t, "one character", "MY======", "f")
t = expect_encode(t, "two characters", "fo", "MZXQ====")
t = expect_decode(t, "two characters", "MZXQ====", "fo")
t = expect_encode(t, "three characters", "foo", "MZXW6===")
t = expect_decode(t, "three characters", "MZXW6===", "foo")
t = expect_encode(t, "four characters", "foob", "MZXW6YQ=")
t = expect_decode(t, "four characters", "MZXW6YQ=", "foob")
t = expect_encode(t, "five characters", "fooba", "MZXW6YTB")
t = expect_decode(t, "five characters", "MZXW6YTB", "fooba")
t = expect_encode(t, "six characters", "foobar", "MZXW6YTBOI======")
t = expect_decode(t, "six characters", "MZXW6YTBOI======", "foobar")
Test.end(t)
}
run_tests()