some tests for fuchsia writer

This commit is contained in:
Simon Cruanes 2023-12-25 18:56:57 -05:00
parent 6eced76971
commit f08850cda8
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
4 changed files with 148 additions and 0 deletions

4
test/fuchsia/write/dune Normal file
View file

@ -0,0 +1,4 @@
(tests
(names t1 t2)
(libraries trace-fuchsia.write))

65
test/fuchsia/write/t1.ml Normal file
View file

@ -0,0 +1,65 @@
open Trace_fuchsia_write
module Str_ = struct
open String
let to_hex (s : string) : string =
let i_to_hex (i : int) =
if i < 10 then
Char.chr (i + Char.code '0')
else
Char.chr (i - 10 + Char.code 'a')
in
let res = Bytes.create (2 * length s) in
for i = 0 to length s - 1 do
let n = Char.code (get s i) in
Bytes.set res (2 * i) (i_to_hex ((n land 0xf0) lsr 4));
Bytes.set res ((2 * i) + 1) (i_to_hex (n land 0x0f))
done;
Bytes.unsafe_to_string res
let of_hex_exn (s : string) : string =
let n_of_c = function
| '0' .. '9' as c -> Char.code c - Char.code '0'
| 'a' .. 'f' as c -> 10 + Char.code c - Char.code 'a'
| 'A' .. 'F' as c -> 10 + Char.code c - Char.code 'A'
| _ -> invalid_arg "string: invalid hex"
in
if String.length s mod 2 <> 0 then
invalid_arg "string: hex sequence must be of even length";
let res = Bytes.make (String.length s / 2) '\x00' in
for i = 0 to (String.length s / 2) - 1 do
let n1 = n_of_c (String.get s (2 * i)) in
let n2 = n_of_c (String.get s ((2 * i) + 1)) in
let n = (n1 lsl 4) lor n2 in
Bytes.set res i (Char.chr n)
done;
Bytes.unsafe_to_string res
end
let () =
let l = List.init 100 (fun i -> Util.round_to_word i) in
assert (List.for_all (fun x -> x mod 8 = 0) l)
let () =
assert (Str_ref.inline 0 = 0b0000_0000_0000_0000);
assert (Str_ref.inline 1 = 0b1000_0000_0000_0001);
assert (Str_ref.inline 6 = 0b1000_0000_0000_0110);
assert (Str_ref.inline 31999 = 0b1111_1100_1111_1111);
()
let () =
let buf = Buf.create 128 in
Buf.add_i64 buf 42L;
assert (Buf.to_string buf = "\x2a\x00\x00\x00\x00\x00\x00\x00")
let () =
let buf = Buf.create 128 in
Buf.add_string buf "";
assert (Buf.to_string buf = "")
let () =
let buf = Buf.create 128 in
Buf.add_string buf "hello";
assert (Buf.to_string buf = "hello\x00\x00\x00")

View file

@ -0,0 +1,4 @@
first trace
100004467854160033000500000000000100000000000000560000000000000054001005000005804e61bc000000000068656c6c6f000000210001802a0000007800000000000000
second trace
1000044678541600210000000000000000ca9a3b00000000330005000000000001000000000000005600000000000000300011000000b0006f63616d6c2d747261636500000000004400040500000580a0860100000000006f75746572000000404b4c0000000000440004050000058020bf020000000000696e6e657200000020aa44000000000054001005000005804e61bc000000000068656c6c6f000000210001802a0000007800000000000000

75
test/fuchsia/write/t2.ml Normal file
View file

@ -0,0 +1,75 @@
open Trace_fuchsia_write
let pf = Printf.printf
module Str_ = struct
open String
let to_hex (s : string) : string =
let i_to_hex (i : int) =
if i < 10 then
Char.chr (i + Char.code '0')
else
Char.chr (i - 10 + Char.code 'a')
in
let res = Bytes.create (2 * length s) in
for i = 0 to length s - 1 do
let n = Char.code (get s i) in
Bytes.set res (2 * i) (i_to_hex ((n land 0xf0) lsr 4));
Bytes.set res ((2 * i) + 1) (i_to_hex (n land 0x0f))
done;
Bytes.unsafe_to_string res
let of_hex_exn (s : string) : string =
let n_of_c = function
| '0' .. '9' as c -> Char.code c - Char.code '0'
| 'a' .. 'f' as c -> 10 + Char.code c - Char.code 'a'
| 'A' .. 'F' as c -> 10 + Char.code c - Char.code 'A'
| _ -> invalid_arg "string: invalid hex"
in
if String.length s mod 2 <> 0 then
invalid_arg "string: hex sequence must be of even length";
let res = Bytes.make (String.length s / 2) '\x00' in
for i = 0 to (String.length s / 2) - 1 do
let n1 = n_of_c (String.get s (2 * i)) in
let n2 = n_of_c (String.get s ((2 * i) + 1)) in
let n = (n1 lsl 4) lor n2 in
Bytes.set res i (Char.chr n)
done;
Bytes.unsafe_to_string res
end
let () = pf "first trace\n"
let () =
let buf = Buf.create 128 in
Metadata.Magic_record.encode buf;
Thread_record.encode buf ~as_ref:5 ~pid:1 ~tid:86 ();
Event.Instant.encode buf ~name:"hello" ~time_ns:1234_5678L
~t_ref:(Thread_ref.Ref 5)
~args:[ "x", `Int 42 ]
();
pf "%s\n" (Buf.to_string buf |> Str_.to_hex)
let () = pf "second trace\n"
let () =
let buf = Buf.create 512 in
Metadata.Magic_record.encode buf;
Metadata.Initialization_record.(
encode buf ~ticks_per_secs:default_ticks_per_sec ());
Thread_record.encode buf ~as_ref:5 ~pid:1 ~tid:86 ();
Metadata.Provider_info.encode buf ~id:1 ~name:"ocaml-trace" ();
Event.Duration_complete.encode buf ~name:"outer" ~t_ref:(Thread_ref.Ref 5)
~time_ns:100_000L ~end_time_ns:5_000_000L ~args:[] ();
Event.Duration_complete.encode buf ~name:"inner" ~t_ref:(Thread_ref.Ref 5)
~time_ns:180_000L ~end_time_ns:4_500_000L ~args:[] ();
Event.Instant.encode buf ~name:"hello" ~time_ns:1_234_567L
~t_ref:(Thread_ref.Ref 5)
~args:[ "x", `Int 42 ]
();
(let oc = open_out "foo.fxt" in
output_string oc (Buf.to_string buf);
close_out oc);
pf "%s\n" (Buf.to_string buf |> Str_.to_hex)