stdlib_tests.aes
1 // Collection of non-exhaustive tests. Feel free to add stuff here. 2 3 include "Func.aes" 4 include "Option.aes" 5 include "List.aes" 6 include "ListInternal.aes" 7 include "Pair.aes" 8 include "Set.aes" 9 include "Triple.aes" 10 11 contract StdTest = 12 function assert(p) = switch(p) 13 (t, msg) => if(t) () else abort(msg) 14 15 16 // dear programmer from the future – this was written when `==` operator 17 // was not working on any ADT. If you are capable of doing it better – do it. 18 function list_int_cmp(l1 : list(int), l2 : list(int)) = switch((l1, l2)) 19 ([], []) => true 20 (h1::t1, h2::t2) => h1 == h2 && list_int_cmp(t1, t2) 21 _ => false 22 23 function option_int_cmp(a : option(int), b) = switch((a, b)) 24 (None, None) => true 25 (Some(x), Some(y)) => x == y 26 _ => false 27 28 function list_int_string_cmp(l1 : list(int * string), l2 : list(int * string)) = switch((l1, l2)) 29 ([], []) => true 30 ((i1, s1)::t1, (i2, s2)::t2) => s1 == s2 && i1 == i2 && list_int_string_cmp(t1, t2) 31 _ => false 32 33 entrypoint test() = List.foreach( 34 [ (Func.id(1) == 1, "id") 35 , (Func.const(1)(0) == 1, "const") 36 , (Func.uncurry2(Func.const)(1,0) == 1, "uncurry2") 37 , (Func.flip(Func.uncurry2(Func.const))(0,1) == 1, "flip") 38 , (Func.curry2((x, y) => x + y)(0)(1) == 1, "curry") 39 , (Func.recur((r, n) => if (n < 2) 1 else n * r(n-1))(5) == 120, "recursor") 40 , (Func.iter(5, (x) => x + 1)(0) == 5, "iter") 41 , (Func.tuplify2((a, b) => a + b)((2, 3)) == 5, "tuplify") 42 43 , (List.is_empty([]), "empty") 44 , (option_int_cmp(List.first([1..4]), Some(1)), "list first") 45 , (option_int_cmp(List.find((x) => x > 0, [-1, -2, 3, -1]), Some(3)), "find") 46 , (list_int_cmp(List.replace_at(2, 0, [1,1,1,1,1]), [1,1,0,1,1]), "replace at") 47 , (list_int_cmp(List.from_to(1, 5), [1..5]), "fromto") 48 , (list_int_cmp(List.foldr((e, cont) => (x) => cont(e::x), (x) => x, [1,2,3])([]), [3,2,1]), "foldr") 49 , (List.foldl((a, x) => a - x, 0, [1..3]) == -6, "foldl") 50 , (list_int_cmp(List.reverse([1,2,3]), [3,2,1]), "reverse") 51 , (list_int_cmp(List.filter((x) => x > 0, [-1,1,0,-2,2]), [1,2]), "filter") 52 , (list_int_cmp(List.take(2, List.drop(2, [1..6])), [3,4]), "drop&take") 53 , (list_int_cmp(List.take_while( 54 (x) => x > 0, 55 List.drop_while( 56 (x) => x < 0, 57 [-1,2,-3,4,-5,6] 58 )), [2]), "drop&take while") 59 , (list_int_string_cmp(List.zip([1..4], ["a", "b"]), [(1, "a"), (2, "b")]), "zip") 60 , (Func.tuplify2(list_int_cmp)(List.unzip([(1,1), (2,2)])), "unzip&tuplify") 61 , (list_int_cmp(List.sort((a, b) => a < b, [3,2,4,1,5]), [1,2,3,4,5]), "sort") 62 , (list_int_cmp(List.intersperse(0, [1,2,3,4]), [1,0,2,0,3,0,4]), "intersperse") 63 , (list_int_string_cmp(List.enumerate(["a", "b"]), [(0, "a"), (1, "b")]), "enumerate") 64 65 , (Option.is_some(Some("hand")), "is_(hand)some") 66 , (Option.default(1, Some(2)) == 2, "default some") 67 , (Option.default(1, None) == 1, "default none") 68 , (option_int_cmp(Option.map2((a, b) => a + b, Some(1), Some(2)), Some(3)), "map2") 69 , (option_int_cmp(Option.app_over( 70 Option.map((x) => (y) => x + y, Some(1)), 71 Some(2)), Some(3)), "(+) <$> Just 1 <*> Just 2") 72 , (list_int_cmp(Option.filter_options([Some(1), None, Some(2)]), [1,2]), "filter options") 73 , (option_int_cmp(Option.choose_first([None, Some(1), None, Some(2)]), Some(1)), "choose fst") 74 , (Set.size(Set.new()) == 0, "size of new set is 0") 75 , (Set.member("x", Set.from_list(["x", "y", "z"])) == true, "set member found") 76 , (Set.member(4, Set.from_list([1, 2, 3])) == false, "set member not found") 77 , (List.sort((x, y) => x < y, Set.to_list(Set.from_list([1, 2, 3]))) == [1, 2, 3], "set-to-list and list-to-set conversion") 78 , (List.sort((x, y) => x < y, Set.to_list(Set.insert(4, Set.from_list([1, 2, 3])))) == [1, 2, 3, 4], "set insert") 79 , (List.sort((x, y) => x < y, Set.to_list(Set.insert(3, Set.from_list([1, 2, 3])))) == [1, 2, 3], "set insert duplicate") 80 , (List.sort((x, y) => x < y, Set.to_list(Set.delete(4, Set.from_list([1, 2, 3])))) == [1, 2, 3], "set delete non-member") 81 , (List.sort((x, y) => x < y, Set.to_list(Set.delete(4, Set.from_list([1, 2, 3, 4])))) == [1, 2, 3], "set delete") 82 , (List.sort((x, y) => x < y, Set.to_list(Set.filter((x) => x > 5, Set.from_list([4, 8, 8])))) == [8], "set filter") 83 , (Set.fold((x, y) => x + y, 0, Set.from_list([1, 2, 3, 4])) == 10, "set fold") 84 , (Set.subtract(Set.from_list([1, 2, 3]), Set.from_list([2, 3, 4])) == Set.from_list([1]), "set subtract") 85 , (Set.intersection_list([Set.from_list([1, 3]), Set.from_list([2, 3]), Set.from_list([3, 3])]) == Set.from_list([3]), "set intersection") 86 , (Set.union_list([Set.from_list([1]), Set.from_list([2]), Set.from_list([3])]) == Set.from_list([1, 2, 3]), "set union") 87 ], assert)