Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/address_book.proto
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(*
(*
The protocol below corresponds to this Protocol Buffers definition, taken from
its tutorial:

Expand Down
4 changes: 2 additions & 2 deletions examples/simple.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ type meta 'a = Unset | Set source 'a

type dim = (int * int)

message metadata =
message metadata =
Text {
author: meta<string>;
pages : meta<int>
}
| Image {
dims : meta<dim>;
colors : meta<int>
colors : meta<int>
}

type tup 'a 'b = ('a * 'b)
Expand Down
14 changes: 7 additions & 7 deletions examples/test_address_book.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ open Extprot
open Address_book
open Printf

let names =
[| "John"; "Jacob"; "Michael"; "Ethan"; "Joshua";
let names =
[| "John"; "Jacob"; "Michael"; "Ethan"; "Joshua";
"Daniel"; "Christopher"; "Anthony"; "William"; "Matthew"; |]

let surnames =
[| "Smith"; "Johnson"; "Williams"; "Brown"; "Jones";
let surnames =
[| "Smith"; "Johnson"; "Williams"; "Brown"; "Jones";
"Miller"; "Davis"; "García"; "Rodríguez"; "Wilson" |]

let phone_type = [|Phone_type.Mobile; Phone_type.Home; Phone_type.Work|]
Expand All @@ -20,17 +20,17 @@ let name_to_s (n, s) = n ^ " " ^ s
let random_email (n, s) =
if Random.bool () then Optional.Unset
else Optional.Set (n ^ string_of_int (Random.int 100) ^ "@" ^ s ^ ".name")
let random_phone () =
let random_phone () =
(sprintf "%d-%d" (Random.int 1000) (Random.int 10000), pick phone_type)
let random_list ?(max_len = 10) f =
Array.to_list (Array.init (Random.int max_len) (fun _ -> f ()))

let random_person () =
let n = random_name () in
{ Person.name = name_to_s n; id = Random.int 100000;
{ Person.name = name_to_s n; id = Random.int 100000;
email = random_email n; phones = random_list random_phone }

let address_book len =
let address_book len =
{
Address_book.persons = random_list ~max_len:len random_person
}
Expand Down
30 changes: 15 additions & 15 deletions examples/test_grafff.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ let circle1 = Shape.Circle { Shape.center = (2, 2); radius = 1 }

let grafff_0_0_1 = { Grafff.objects = [ (triangle1, 100); (circle1, 0); ] }

let grafff_1_0 =
{
Grafff1.objects = [ (triangle1, 100, Brush.Hello_Kitty 10);
(circle1, 0, Brush.Calligraphic 4); ]
let grafff_1_0 =
{
Grafff1.objects = [ (triangle1, 100, Brush.Hello_Kitty 10);
(circle1, 0, Brush.Calligraphic 4); ]
}

let grafff_2_0 =
{
Grafff2.objects =
let grafff_2_0 =
{
Grafff2.objects =
[
(triangle1, bright_red, Brush.Hello_Kitty 10, Filling.No_filling);
(circle1, black, Brush.Calligraphic 4, Filling.Alpha_filling 50);
]
(triangle1, bright_red, Brush.Hello_Kitty 10, Filling.No_filling);
(circle1, black, Brush.Calligraphic 4, Filling.Alpha_filling 50);
]
}

let write = Conv.serialize
Expand All @@ -36,7 +36,7 @@ let aeq pp ?(msg = "") expected actual =
msg pp expected pp actual
end

let check () =
let check () =
let s0 = write Grafff.write_grafff grafff_0_0_1 in
let s1 = write Grafff1.write_grafff1 grafff_1_0 in
let s2 = write Grafff2.write_grafff2 grafff_2_0 in
Expand All @@ -48,19 +48,19 @@ let check () =
aeq Grafff.pp_grafff g0 g0'';
(* default values for missing new fields *)
let g1' = read Grafff1.read_grafff1 s0 in
aeq Grafff1.pp_grafff1
aeq Grafff1.pp_grafff1
{ Grafff1.objects = [(triangle1, 100, Brush.Default); (circle1, 0, Brush.Default)] }
g1';
(* primitive types expanded to composed types (tuples) *)
let g2' = read Grafff2.read_grafff2 s1 in
aeq Grafff2.pp_grafff2
{ Grafff2.objects =
{ Grafff2.objects =
[(triangle1, hsv_0_0_100, Brush.Hello_Kitty 10, Filling.No_filling);
(circle1, black, Brush.Calligraphic 4, Filling.No_filling)] }
(circle1, black, Brush.Calligraphic 4, Filling.No_filling)] }
g2';
()

let () =
let () =
try check ()
with Error.Extprot_error(err, loc) ->
Format.printf "Extprot error:@.%a@." Error.pp_extprot_error (err, loc)
30 changes: 15 additions & 15 deletions test/inspect_msg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class BadWireType < ExtprotError; end
class UnknownTag < ExtprotError; end

module Codec
@@types = [
:vint, :tuple, :bits8, :bytes,
@@types = [
:vint, :tuple, :bits8, :bytes,
:bits32, :htuple, :bits64_long, :assoc,
:bits64_float, :invalid, :enum, :invalid,
:invalid, :invalid, :invalid, :invalid
:invalid, :invalid, :invalid, :invalid
]

def ll_type(n); @@types[n & 0xf] end
Expand All @@ -20,7 +20,7 @@ def ll_tag(n); n >> 4 end
module_function :ll_type, :ll_tag
end

class Reader
class Reader
include Codec

def initialize(io)
Expand All @@ -33,7 +33,7 @@ def read_vint
raise EOFError unless b
return b if b < 128
x = e = 0
while b >= 128
while b >= 128
x += (b - 128) << e
e += 7
b = read_byte
Expand All @@ -44,18 +44,18 @@ def read_vint

alias_method :read_prefix, :read_vint

def read_bytes(n);
r = @io.read(n)
def read_bytes(n);
r = @io.read(n)
@off += r.size
r
end

def read_byte;
def read_byte;
@off += 1
@io.getc
@io.getc
end

def check_prim_type(ty, t)
def check_prim_type(ty, t)
p = read_prefix
if ll_tag(p) != 0 then
skip_value(p)
Expand All @@ -71,7 +71,7 @@ def read_prim_type(ty, &b)
p = read_prefix
llty = ll_type(p)
puts "prefix #{p} type #{llty}"
if ll_tag(p) == 0 && llty == ty
if ll_tag(p) == 0 && llty == ty
b.call
elsif ll_tag(p) != 0
skip_value p
Expand Down Expand Up @@ -109,7 +109,7 @@ def skip_value(p)
when :bits8; read_byte
when :bits32; read_bytes(4)
when :bits64_float, :bits64_long; read_bytes(8)
when :enum;
when :enum;
when :tuple, :htuple, :bytes, :assoc; @rd.read(read_vint)
when :invalid; raise BadWireType
end
Expand Down Expand Up @@ -154,7 +154,7 @@ def skip_to(off)
end

class Inspect
include Codec
include Codec

def initialize(reader, out, verbose = true, maxwidth = 79)
@rd = reader
Expand All @@ -172,12 +172,12 @@ def inspect

def doinspect(prefix)
tag = ll_tag(prefix)
case ll_type(prefix)
case ll_type(prefix)
when :tuple; inspect_tuple("{ ", " }", prefix)
when :htuple; inspect_tuple("[ ", " ]", prefix)
when :assoc; inspect_assoc(prefix)
when :vint
if @verbose || tag != 0
if @verbose || tag != 0
@out.text("Vint_%d %d" % [tag, @rd.read_raw_rel_int])
else
@out.text("%d" % @rd.read_raw_rel_int)
Expand Down