Skip to content

Commit

Permalink
GH-142: Migrate added tests of Int
Browse files Browse the repository at this point in the history
  • Loading branch information
zeptometer committed Mar 3, 2024
1 parent 46d02a8 commit 6a21bb6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 91 deletions.
41 changes: 0 additions & 41 deletions __test__/satysrc/generic.saty
Original file line number Diff line number Diff line change
Expand Up @@ -142,47 +142,6 @@ let rules = [
let (Some tokens) = Lexer.lex rules `abc defg hijklMNL op123 ` in
let () = tokens |> List.iter (fun tk -> (Debug.log (`(| kind = `# ^ tk#kind ^ `, data = "`# ^ tk#data ^ `" |)`))) in

let () = Debug.log `==== Int ====` in
let p i = i |> String.of-int |> Debug.log in
let () = p (13 |> Int.lsl 2) in %==52
let () = p (13 |> Int.asr 0) in %==13
let () = p (13 |> Int.asr 1) in %==6
let () = p (13 |> Int.asr 2) in %==3
let () = p (13 |> Int.asr 3) in %==1
let () = p (13 |> Int.asr 4) in %==0
let () = p (13 |> Int.asr 5) in %==0
let () = p (-13 |> Int.asr 0) in %==-13
let () = p (-13 |> Int.asr 1) in %==-7
let () = p (-13 |> Int.asr 2) in %==-4
let () = p (-13 |> Int.asr 3) in %==-2
let () = p (-13 |> Int.asr 4) in %==-1
let () = p (-13 |> Int.asr 5) in %==-1
let () = p (13 |> Int.land 10) in %==8
let () = p (-13 |> Int.land 31) in %==19
let () = p (13 |> Int.lor 10) in %==15
let () = p (13 |> Int.lxor 10) in %==7
let () = p (13 |> Int.lsr 2) in %==3
let () = p (-13 |> Int.lsr 2) in %==2305843009213693948
let () = p (`12345678` |> Int.of-string ?:10) in
let () = p (`-123` |> Int.of-string ?:10) in
let () = p (`ffff` |> Int.of-string ?:16) in
let () = p (2 |> Int.pow 3) in
let () = p (2 |> Int.pow 0) in
let () = p (Int.sup [1; 3; 2]) in
let () = p (Int.sup []) in
let p-opt o =
let s =
match o with
| None -> `None`
| Some(i) -> `Some(` ^ Int.to-string i ^ `)`
in
Debug.log s
in
let () = p-opt(`123` |> Int.of-string-opt ?:10) in
let () = p-opt(`-123` |> Int.of-string-opt ?:10) in
let () = p-opt(`12n` |> Int.of-string-opt ?:10) in
let () = p-opt(`a` |> Int.of-string-opt ?:16) in

let () = Debug.log `==== Map ====` in
let m = Map.of-list [(1, `a`); (2, `b`); (3, `c`)] in
let option-force (Some v) = v in
Expand Down
74 changes: 24 additions & 50 deletions test/int.test.satyg
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
@import: ../src/option-ext

let int-test-cases = open Test in
let intleq = Eq.equal (List.eq Int.eq) in
let intoeq = Eq.equal (Option.eq Int.eq) in
describe `Int module` [
it `calculate logical shift left` (fun () -> (
13
|> Int.lsl 2
|> (==) 52
(13 |> Int.lsl 2) == 52
|> Expect.is-true
));
it `calculate arithmetic shift right of positive` (fun () -> (
[0; 1; 2; 3; 4; 5]
|> List.map (fun x -> 13 |> Int.asr x)
|> Eq.equal (List.eq Int.eq) [13; 6; 3; 1; 0; 0]
|> intleq [13; 6; 3; 1; 0; 0]
|> Expect.is-true
));
it `calculate arithmetic shift right of negative` (fun () -> (
[0; 1; 2; 3; 4; 5]
|> List.map (fun x -> -13 |> Int.asr x)
|> Eq.equal (List.eq Int.eq) [-13; -7; -4; -2; -1; -1]
|> intleq [-13; -7; -4; -2; -1; -1]
|> Expect.is-true
));
it `calculate logical and` (fun () -> (
Expand All @@ -31,83 +31,57 @@ let int-test-cases = open Test in
|> Expect.is-true
));
it `calculate logical or` (fun () -> (
10
|> Int.lor 13
|> (==) 15
(10 |> Int.lor 13) == 15
|> Expect.is-true
));
it `calculate logical exclusive or` (fun () -> (
10
|> Int.lxor 13
|> (==) 7
(10 |> Int.lxor 13) == 7
|> Expect.is-true
));
it `calculate logical shift right` (fun () -> (
(13
|> Int.lsr 2
|> (==) 3)
&& (-13
|> Int.lsr 2
|> (==) 2305843009213693948)
(13 |> Int.lsr 2) == 3
&& (-13 |> Int.lsr 2) == 2305843009213693948
|> Expect.is-true
));
it `parse decimal number` (fun () -> (
`12345678`
|> Int.of-string ?:10
|> (==) 12345678
|> Expect.is-true
));
it `parse negative decimal number` (fun () -> (
`-123`
|> Int.of-string ?:10
|> (==) (-123)
|> Expect.is-true
));
it `parse hexadecimal number` (fun () -> (
`ffff`
|> Int.of-string ?:16
|> (==) 65535
it `parse number with given base` (fun () -> (
(`12345678` |> Int.of-string ?:10) == 12345678
&& (`-123` |> Int.of-string ?:10) == -123
&& (`ffff` |> Int.of-string ?:16) == 65535
|> Expect.is-true
));
it `calculate power` (fun () -> (
(2 |> Int.pow 3 |> (==) 8)
&& (2 |> Int.pow 0 |> (==) 1)
(2 |> Int.pow 3) == 8
&& (2 |> Int.pow 0) == 1
|> Expect.is-true
));
it `calculate supreme from a list of numbers` (fun () -> (
Int.sup [1; 3; 2]
|> (==) 3
(Int.sup [1; 3; 2]) == 3
|> Expect.is-true
));
it `calculate supreme from an empty list` (fun () -> (
%% SATySFi compiler fails to parse -4611686018427387904
let expected = Int.of-string ?:10 `-4611686018427387904` in
Int.sup []
|> (==) expected
(Int.sup []) == expected
|> Expect.is-true
));
it `parse maybe decimal` (fun () -> (
`123`
|> Int.of-string-opt ?:10
`123` |> Int.of-string-opt ?:10
|> Eq.equal (Option.eq Int.eq) (Option.some 123)
|> Expect.is-true
));
it `parse maybe negative decimal` (fun () -> (
`-123`
|> Int.of-string-opt ?:10
|> Eq.equal (Option.eq Int.eq) (Option.some (-123))
`-123` |> Int.of-string-opt ?:10
|> intoeq (Option.some (-123))
|> Expect.is-true
));
it `fail to parse non decimal` (fun () -> (
`12n`
|> Int.of-string-opt ?:10
|> Eq.equal (Option.eq Int.eq) Option.none
`12n` |> Int.of-string-opt ?:10
|> intoeq Option.none
|> Expect.is-true
));
it `parse maybe hexadecimal` (fun () -> (
`a`
|> Int.of-string-opt ?:16
|> Eq.equal (Option.eq Int.eq) (Option.some 10)
`a` |> Int.of-string-opt ?:16
|> intoeq (Option.some 10)
|> Expect.is-true
));
% TODO
Expand Down

0 comments on commit 6a21bb6

Please sign in to comment.