3 * _| || |_/ ___| ___ _ __ _ __ ___ | |
4 * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
5 * |_ _|___) | __/ |_) | |_) | (_) |_|
6 * |_||_| |____/ \___| .__/| .__/ \___/(_)
11 * Copyright (C) The #Seppo contributors. All rights reserved.
13 * This program is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 Logr.info
(fun m
-> m
"test_scanf");
31 (let tup2 a b
= (a
, b
) in
32 (let c, t
= Scanf.sscanf
"4 2022-04-06T14:55:17Z" "%i %s" tup2 in
33 Assrt.equals_string __LOC__
"2022-04-06T14:55:17Z" t
;
34 Assrt.equals_int __LOC__
4 c);
36 let _ = Scanf.sscanf
"fail 2022-04-06T14:55:17Z" "%i %s" tup2 in
38 with Scanf.Scan_failure _msg
->
39 Assrt.equals_string __LOC__
40 "scanf: bad input at char number 0: character 'f' is not a decimal digit"
45 Logr.info
(fun m
-> m
"test_hash");
46 let lut = Hashtbl.create
20 in
47 Hashtbl.add
lut "127.0.0.1" (1, Ptime.epoch
);
49 match Hashtbl.find
lut "127.0.0.1" with
52 assert (tim
= Ptime.epoch
)
53 with Not_found
-> assert false
60 Sexplib0.Sexp.Atom adr_
;
61 Sexplib0.Sexp.Atom sev_
;
62 Sexplib0.Sexp.Atom tim_
;
67 Option.value ~default
:Ptime.epoch
68 (Ptime.of_float_s
(float_of_string tim_
)) )
69 | _ -> Error
"Cannot parse Csexp"
71 let sexp_of_t (adr
, sev
, tim
) =
74 Sexplib0.Sexp.Atom adr
;
75 Sexplib0.Sexp.Atom
(string_of_int sev
);
76 Sexplib0.Sexp.Atom
(string_of_float
(Ptime.to_float_s tim
));
79 module Csexp
= Csexp.Make
(Sexplib0.Sexp
)
82 Logr.info
(fun m
-> m
"test_csexp");
86 Option.value ~default
:Ptime.epoch
(Ptime.of_float_s
1605912887.3501091) )
89 let s = sexp_of_t ban |> Csexp.to_string
in
90 Assrt.equals_string __LOC__
"(9:127.0.0.11:413:1605912887.35)" s;
92 match s |> Csexp.parse_string
|> t_of_sexp with
93 | Ok
(adr'
, sev'
, tim'
) ->
94 let adr, sev
, tim
= ban in
95 Assrt.equals_string __LOC__
adr adr'
;
96 Assrt.equals_int __LOC__ sev sev'
;
97 Assrt.equals_float __LOC__
(Ptime.to_float_s tim
) (Ptime.to_float_s tim'
)
103 Logr.info
(fun m
-> m
"test_seq");
104 Printf.sprintf
"%.0f" 1.0
105 |> Assrt.equals_string __LOC__
"1";
106 let fn = "tmp/seq.cdb" in
108 Unix.mkdir
"tmp" File.pDir
;
109 with Unix.Unix_error
(Unix.EEXIST
, _, _) -> ());
112 Unix.unlink
(fn ^
"~")
113 with Unix.Unix_error
(Unix.ENOENT
, _, _) -> ());
115 let cdb = Mapcdb.Cdb
fn
117 and t0
= Ptime.epoch
in
118 let f x
= x
*. Ban.chunk_s
|> Ptime.Span.of_float_s
|> Option.get
|> Ptime.add_span t0
|> Option.get
in
124 Logr.info
(fun m
-> m
"test_seq 1");
125 Assrt.equals_none __LOC__
(Ban.check
cdb t0 k
);
126 Logr.info
(fun m
-> m
"test_seq 10");
127 Ban.escalate
cdb t1 k
;
128 Mapcdb.find_string_opt k
cdb
129 |> Option.value ~default
:"fail"
130 |> Assrt.equals_string __LOC__
"1970-01-01T00:15:00-00:00";
131 Assrt.equals_none __LOC__
(Ban.check
cdb t1 k
);
132 Ban.escalate
cdb t1 k
;
133 Ban.escalate
cdb t1 k
;
134 Ban.escalate
cdb t1 k
;
135 Ban.check
cdb t1 k
|> Option.get
|> Ptime.to_rfc3339
136 |> Assrt.equals_string __LOC__
"1970-01-01T00:25:00-00:00";
137 Ban.escalate
cdb t4 k
;
138 Assrt.equals_none __LOC__
(Ban.check
cdb t5 k
);
142 Unix.chdir
"../../../test/";