-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment3_tests.sml
103 lines (92 loc) · 3.26 KB
/
assignment3_tests.sml
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
(*
Training test cases for PKD assignment 3 (Cryptography), HT 13
Developed by Dave Clarke
To run these training cases:
1) launch PolyML shell [poly]
2) load hand-in [use "crypto.sml"]
3) load training set [use "assignment3_tests.sml"]
4) run tests [test ()]
*)
(******************************************************************************)
(* To run the code below, the functions preprocess, encrypt, decrypt and *)
(* keystream must be declared in cryptosml and have the correct type. *)
(******************************************************************************)
(* let's make sure these functions are in fact declared and have the correct type *)
preprocess: string -> char list list;
encrypt: char list list -> char list list;
decrypt: char list list -> char list list;
keystream : int -> char list;
(* result (n, f)
TYPE: int * (int -> bool) -> unit
PRE: f is well-defined (i.e., terminates without error) for the call f n
POST: ()
SIDE-EFFECTS: Prints a report stating whether test n was successful or not
(where f n = true iff the test was successful)
*)
fun result (n, f) =
print ("Test #" ^ Int.toString n ^
((if f n then " successful." else " FAILED!")
handle _ =>
" raised an (unwanted) exception!") ^ "\n");
(* test ()
TYPE: unit -> unit
PRE: true
POST: ()
SIDE-EFFECTS: Prints a report, stating whether each test case performed as
expected.
*)
fun test () =
let
(* test n
TYPE: int -> bool
PRE: 1<=n<=4
POST: true iff test n executes correctly
*)
fun test 1 =
let
val input1 = "@#@*(H@#$@#()e@#)9$)l$#@p!!"
val output1 = [[#"H", #"E", #"L", #"P", #"X"]]
val input2 = "Live long and prosper!"
val output2 = [[#"L", #"I", #"V", #"E", #"L"], [#"O", #"N", #"G", #"A", #"N"],
[#"D", #"P", #"R", #"O", #"S"], [#"P", #"E", #"R", #"X", #"X"]]
val input3 = "Ample Juice"
val output3 = [[#"A", #"M", #"P", #"L", #"E"], [#"J", #"U", #"I", #"C", #"E"]]
val input4 = ""
val output4 = []
in
preprocess input1 = output1 andalso
preprocess input2 = output2 andalso
preprocess input3 = output3 andalso
preprocess input4 = output4
end
| test 2 =
let
val input1 = [[#"L", #"I", #"V", #"E", #"L"], [#"O", #"N", #"G", #"A", #"N"],
[#"D", #"P", #"R", #"O", #"S"], [#"P", #"E", #"R", #"X", #"X"]]
val output1 = [[#"P", #"F", #"F", #"C", #"T"], [#"N", #"F", #"M", #"E", #"U"],
[#"X", #"C", #"K", #"W", #"I"], [#"K", #"Z", #"J", #"V", #"H"]]
val input2 = []
val output2 = []
in
encrypt input1 = output1 andalso
encrypt input2 = output2
end
| test 3 =
let
val input1 = [[#"P", #"F", #"F", #"C", #"T"], [#"N", #"F", #"M", #"E", #"U"],
[#"X", #"C", #"K", #"W", #"I"], [#"K", #"Z", #"J", #"V", #"H"]]
val output1 = [[#"L", #"I", #"V", #"E", #"L"], [#"O", #"N", #"G", #"A", #"N"],
[#"D", #"P", #"R", #"O", #"S"], [#"P", #"E", #"R", #"X", #"X"]]
val input2 = []
val output2 = []
in
decrypt input1 = output1 andalso
decrypt input2 = output2
end
| test 4 =
keystream 0 = [] andalso
keystream 7 = [#"D", #"W", #"J", #"X", #"H", #"Y", #"R"]
| test _ = raise Domain
in
List.app result [(1, test), (2, test), (3, test), (4, test)]
end;