Alpha: refactor Tez_repr tests a bit to integrate them with others

This commit is contained in:
Benjamin Canou 2018-10-09 09:51:23 +02:00
parent a250198cfb
commit a475ef701a
No known key found for this signature in database
GPG Key ID: 73607948459DC5F8
4 changed files with 32 additions and 85 deletions

View File

@ -1,41 +0,0 @@
(*****************************************************************************)
(* *)
(* Open Source License *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* Permission is hereby granted, free of charge, to any person obtaining a *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)
(* and/or sell copies of the Software, and to permit persons to whom the *)
(* Software is furnished to do so, subject to the following conditions: *)
(* *)
(* The above copyright notice and this permission notice shall be included *)
(* in all copies or substantial portions of the Software. *)
(* *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)
(* DEALINGS IN THE SOFTWARE. *)
(* *)
(*****************************************************************************)
let fail expected given msg =
Format.kasprintf Pervasives.failwith
"@[%s@ expected: %s@ got: %s@]" msg expected given
let fail_msg fmt = Format.kasprintf (fail "" "") fmt
let default_printer _ = ""
let equal ?(eq=(=)) ?(prn=default_printer) ?(msg="") x y =
if not (eq x y) then fail (prn x) (prn y) msg
let is_none ?(msg="") x =
if x <> None then fail "None" "Some _" msg
let is_some ?(msg="") x =
if x = None then fail "Some _" "None" msg

View File

@ -1,28 +0,0 @@
(executables
(names test_qty_repr)
(libraries tezos-base
tezos-protocol-alpha
tezos-client-alpha
alcotest-lwt)
(flags (:standard -w -9+27-30-32-40@8 -safe-string
-open Tezos_base__TzPervasives
-open Tezos_client_alpha
-open Tezos_protocol_alpha)))
(alias
(name buildtest)
(deps test_qty_repr.exe))
(alias
(name runtest_qty_repr)
(action (run %{exe:test_qty_repr.exe})))
(alias
(name runtest)
(deps (alias runtest_qty_repr)))
(alias
(name runtest_indent)
(deps (glob_files *.ml{,i}))
(action (run bash %{libexec:tezos-stdlib:test-ocp-indent.sh} %{deps})))

View File

@ -36,4 +36,5 @@ let () =
"delegation", Delegation.tests ;
"rolls", Rolls.tests ;
"combined", Combined_operations.tests ;
"qty", Qty.tests ;
]

View File

@ -61,23 +61,41 @@ let known_bad_tez_litterals =
"0.0000000" ;
"9,999,999,999,999.999,999"]
let fail expected given msg =
Format.kasprintf Pervasives.failwith
"@[%s@ expected: %s@ got: %s@]" msg expected given
let fail_msg fmt = Format.kasprintf (fail "" "") fmt
let default_printer _ = ""
let equal ?(eq=(=)) ?(prn=default_printer) ?(msg="") x y =
if not (eq x y) then fail (prn x) (prn y) msg
let is_none ?(msg="") x =
if x <> None then fail "None" "Some _" msg
let is_some ?(msg="") x =
if x = None then fail "Some _" "None" msg
let test_known_tez_litterals () =
List.iter
(fun (v, s) ->
let vv = Tez_repr.of_mutez v in
let vs = Tez_repr.of_string s in
let vs' = Tez_repr.of_string (String.concat "" (String.split_on_char ',' s)) in
let vv = match vv with None -> Assert.fail_msg "could not unopt %Ld" v | Some vv -> vv in
let vs = match vs with None -> Assert.fail_msg "could not unopt %s" s | Some vs -> vs in
let vs' = match vs' with None -> Assert.fail_msg "could not unopt %s" s | Some vs' -> vs' in
Assert.equal ~prn:Tez_repr.to_string vv vs ;
Assert.equal ~prn:Tez_repr.to_string vv vs' ;
Assert.equal ~prn:(fun s -> s) (Tez_repr.to_string vv) s)
let vv = match vv with None -> fail_msg "could not unopt %Ld" v | Some vv -> vv in
let vs = match vs with None -> fail_msg "could not unopt %s" s | Some vs -> vs in
let vs' = match vs' with None -> fail_msg "could not unopt %s" s | Some vs' -> vs' in
equal ~prn:Tez_repr.to_string vv vs ;
equal ~prn:Tez_repr.to_string vv vs' ;
equal ~prn:(fun s -> s) (Tez_repr.to_string vv) s)
known_ok_tez_litterals ;
List.iter
(fun s ->
let vs = Tez_repr.of_string s in
Assert.is_none ~msg:("Unexpected successful parsing of " ^ s) vs)
is_none ~msg:("Unexpected successful parsing of " ^ s) vs)
known_bad_tez_litterals ;
return_unit
@ -85,24 +103,24 @@ let test_random_tez_litterals () =
for _ = 0 to 100_000 do
let v = Random.int64 12L in
let vv = Tez_repr.of_mutez v in
let vv = match vv with None -> Assert.fail_msg "could not unopt %Ld" v | Some vv -> vv in
let vv = match vv with None -> fail_msg "could not unopt %Ld" v | Some vv -> vv in
let s = Tez_repr.to_string vv in
let vs = Tez_repr.of_string s in
let s' = String.concat "" (String.split_on_char ',' s) in
let vs' = Tez_repr.of_string s' in
Assert.is_some ~msg:("Could not parse " ^ s ^ " back") vs ;
Assert.is_some ~msg:("Could not parse " ^ s ^ " back") vs' ;
is_some ~msg:("Could not parse " ^ s ^ " back") vs ;
is_some ~msg:("Could not parse " ^ s ^ " back") vs' ;
begin match vs with
| None -> assert false
| Some vs ->
let rev = Tez_repr.to_int64 vs in
Assert.equal ~prn:Int64.to_string ~msg:(Tez_repr.to_string vv) v rev
equal ~prn:Int64.to_string ~msg:(Tez_repr.to_string vv) v rev
end ;
begin match vs' with
| None -> assert false
| Some vs' ->
let rev = Tez_repr.to_int64 vs' in
Assert.equal ~prn:Int64.to_string ~msg:(Tez_repr.to_string vv) v rev
equal ~prn:Int64.to_string ~msg:(Tez_repr.to_string vv) v rev
end
done ;
return_unit
@ -120,7 +138,4 @@ let wrap (n, f) =
Format.kasprintf Pervasives.failwith "%a" pp_print_error error
end
let () =
Alcotest.run ~argv:[|""|] "tezos-lib-protocol" [
"Qty_repr", List.map wrap tests
]
let tests = List.map wrap tests