Commit 830d7385 authored by Ryo33's avatar Ryo33

Add Power

parent 4bd37245
type previous_block = { algorithm : string; hash : string }
type t = {
version : string;
previous_block : previous_block option;
hash_algorithm : string;
previous_hash : Hash.t option;
hash_algorithm : HashAlgorithm.t;
expires_at : ExpirationDateTime.t;
minimum_required_power_for_next_block : Power.t;
commands : Command.t array;
......
......@@ -5,6 +5,9 @@ type t =
power : Power.t }
| RemovePublicKey of {
fingerprint : string }
| UpdatePower of {
fingerprint : string;
power : Power.t }
| UserCommand of {
protocol : string;
body : string }
......
type t = { algorithm : HashAlgorithm.t; hash : string }
type t = string
type t = {
hash_algorithm : HashAlgorithm.t;
hash : string }
type t = int
type t = { value : int }
let from_int value =
if value > 2147483647 then None else
if value < 0 then None else
Some { value = value }
let to_int { value } = value
type t
val from_int : int -> t option
(** convert int to Power.t *)
val to_int : t -> int
(** convert Power.t to power *)
type t = {
algorithm : string;
body : string }
type t = { algorithm : string; body : string }
(tests
(names test)
(names test power_test)
(libraries alcotest yayakaChain))
open Core
open YayakaChain
module Test = Alcotest
let to_int_option = Option.map ~f:Power.to_int
let to_power value =
match (Power.from_int value) with
| Some power -> power
| _ -> raise (Failure "")
let from_int () =
Test.(check (option int)) "0" (Some 0) (to_int_option (Power.from_int 0));
Test.(check (option int)) "1,000,000" (Some 1000000) (to_int_option (Power.from_int 1000000));
Test.(check (option int)) "2 ^ 31 - 1" (Some 2147483647) (to_int_option (Power.from_int 2147483647));
Test.(check (option int)) "2 ^ 31" None (to_int_option (Power.from_int 2147483648));
Test.(check (option int)) "-1" None (to_int_option (Power.from_int ~- 1));
Test.(check (option int)) "-1,000,000" None (to_int_option (Power.from_int ~- 1000000))
let to_int () =
Test.(check int) "0" 0 (Power.to_int (to_power 0));
Test.(check int) "1,000,000" 1000000 (Power.to_int (to_power 1000000));
Test.(check int) "2 ^ 31 - 1" 2147483647 (Power.to_int (to_power 2147483647))
let power_tests = [
"from_int", `Quick, from_int;
"to_int", `Quick, to_int;
]
let () =
Test.run "Power" [
"Power", power_tests;
]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment