Skip to content

Commit

Permalink
More tests to Map, Set, Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
MinionJakub authored Jun 30, 2024
1 parent 27ff11e commit 799b3ee
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
58 changes: 58 additions & 0 deletions test/stdlib/stdlib0001_Map.fram
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import OrderedMap

let assert condition =
if condition then () else exit 1

let rec eq_list lista_1 lista_2 compare =
match (lista_1,lista_2) with
| ([],[]) => True
| (x::xs, y::ys) => if compare x y then eq_list xs ys compare else False
| _ => False
end

let rec foldl func lista acc =
match lista with
| [] => acc
| x :: xs => func x (foldl func xs acc)
end

let only_val lista = foldl (fn x acc => (snd x) :: acc) lista []

let lt (v1 : Int) (v2 : Int) = v1 < v2

let OrderedMap.Map {module IntMap} = OrderedMap.makeMap lt

let x = IntMap.empty
let _ = assert (x.isEmpty)


let x = x.add 1 1
let _ = assert (x.isEmpty == False)

let x = x.add 2 1 >. add 3 2 >. add 4 3
let _ = assert (eq_list (only_val x.toList) [1,1,2,3] (fn (x : Int) (y : Int) => x == y))

let x = x.update 2 5
let _ = assert (eq_list (only_val x.toList) [1,5,2,3] (fn (x : Int) (y : Int) => x == y))

let x = x.deleteElem 1
let _ = assert (eq_list (only_val x.toList) [5,2,3] (fn (x : Int) (y : Int) => x == y))

let y = IntMap.empty >. add 5 2 >. add 6 1 >. add 1 1 >. add 0 8
let x = x.join y
let _ = assert (eq_list (only_val x.toList) [8,1,5,2,3,2,1] (fn (x : Int) (y : Int) => x == y))

let x = x.deleteMin
let _ = assert (eq_list (only_val x.toList) [1,5,2,3,2,1] (fn (x : Int) (y : Int) => x == y))

let x = x.deleteMax
let _ = assert (eq_list (only_val x.toList) [1,5,2,3,2] (fn (x : Int) (y : Int) => x == y))

let get_value val =
match val with
| Some (x,y) => y
| _ => -1
end

let y = x.find 3
let _ = assert ((fn (x : Int) (y : Int) => x == y) (get_value y) 2)
36 changes: 36 additions & 0 deletions test/stdlib/stdlib0002_Set.fram
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import OrderedMap

let assert condition =
if condition then () else exit 1

let rec eq_list lista_1 lista_2 compare =
match (lista_1,lista_2) with
| ([],[]) => True
| (x::xs, y::ys) => if compare x y then eq_list xs ys compare else False
| _ => False
end

let rec foldl func lista acc =
match lista with
| [] => acc
| x :: xs => func x (foldl func xs acc)
end

let lt (v1 : Int) (v2 : Int) = v1 < v2

let OrderedMap.Set {module IntSet} = OrderedMap.makeSet lt

let x = IntSet.empty
let _ = assert x.isEmpty

let x = x.insert 1
let _ = assert (x.isEmpty == False)

let x = x.insert 2 >. insert 3 >. insert 2
let _ = assert (x.find 2 && x.find 1 && x.find 3 && x.find 4 == False)

let x = 4.singleton
let _ = assert ((x.find 2 && x.find 1 && x.find 3 && x.find 4 == False) == False)

let x = x.deleteMax
let _ = assert (x.isEmpty)
23 changes: 23 additions & 0 deletions test/stdlib/stdlib0003_Queue.fram
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Queue


let assert condition =
if condition then () else exit 1

let compare (x : Int) (y : Int) = x == y
let get_val x =
match x with
| Some x => x
| _ => -1
end

let Queue.Queue {module IntQ} = Queue.makeQueue

let x = IntQ.empty
let _ = assert x.isEmpty
let x = x.push 1
let _ = assert (x.isEmpty == False && compare (get_val x.head) 1)
let x = x.pop
let _ = assert x.isEmpty
let x = x >. push 1 >. push 2 >. push 3
let _ = assert (x.isEmpty == False && compare (get_val x.head) 1 && compare (get_val (x.pop >. head)) 2 && compare (get_val (x.pop >. pop >. head)) 3)

0 comments on commit 799b3ee

Please sign in to comment.