kahl

R⁷RS Scheme parser combinator library for decoding BARE messages

git clone https://git.8pit.net/kahl.git

 1(test-group "uint"
 2  (test-parse 0   parse-uint #u8(0))
 3  (test-parse 300 parse-uint #u8(#xac #x2))
 4
 5  (test-parse 12418871010253280812 parse-uint
 6              #u8(#xac #xac #xac #xac #xac #xac #xac #xac #xac #x01))
 7
 8  ;; 64-bit precision exceeded.
 9  (test-parse-error "failed predicate" parse-uint
10    #u8(#xff #xff #xff #xff #xff #xff #xff #xff #xff #x2c))
11
12  ;; Too many octets.
13  (test-parse-error "failed predicate" parse-uint
14              #u8(#xac #xac #xac #xac #xac #xac #xac #xac #xac #xac #x2))
15
16  (test-parse-error "failed predicate" parse-uint #u8(#xac))
17  (test-parse-error "failed predicate" parse-uint #u8(#xac #xac)))
18
19(test-group "int"
20  (test-parse 0  parse-int #u8(0))
21  (test-parse -1 parse-int #u8(1))
22  (test-parse 1  parse-int #u8(2))
23  (test-parse -2 parse-int #u8(3)))
24
25(test-group "u8, u16, u32, u64"
26  (test-parse 0      parse-u8 #u8(0))
27  (test-parse 255    parse-u8 #u8(#xff))
28  (test-parse 42     parse-u8 #u8(42))
29  (test-parse #x4223 parse-u16 #u8(#x23 #x42))
30  (test-parse 65535  parse-u16 #u8(#xff #xff))
31
32  (test-parse-error "unexpected eof" parse-u16 #u8(1))
33  (test-parse-error "unexpected eof" parse-u32 #u8(1 2)))
34
35(test-group "i8, i16, i32, i64"
36  (test-parse 126    parse-i8  #u8(#x7e))
37  (test-parse -128   parse-i8  #u8(#x80))
38
39  (test-parse 4242424242 parse-i64 #u8(#xb2 #x41 #xde #xfc #x00 #x00 #x00 #x00)))
40
41(test-group "bool"
42  (test-parse #f parse-bool #u8(0))
43  (test-parse #t parse-bool #u8(1))
44
45  (test-parse-error "invalid boolean value"
46                    parse-bool #u8(#xff)))
47
48(test-group "enum"
49  (test-parse 42 (parse-enum '(23 42 1337)) #u8(42))
50  (test-parse-error "enum value not part of given list"
51                    (parse-enum '(42 23)) #u8(5))
52
53  ;; enum value list must be non-empty
54  (test-error (parse-enum '()))
55
56  ;; enum values must be inuque
57  (test-error (parse-enum '(23 23))))
58
59(test-group "string"
60  (test-parse "foo" parse-string
61              (bytevector-append
62                #u8(3)
63                (string->utf8 "foo"))))
64
65(test-group "data<length>"
66  (test-parse #u8(1 2 3 4 5) (parse-data 5) #u8(1 2 3 4 5))
67  (test-parse #u8(1) (parse-data 1) #u8(1))
68
69  ;; length of fixed-length data must be at least 1
70  (test-error (parse-data 0)))
71
72(test-group "data"
73  (test-parse #u8(1 2 3 4 5) (parse-data) #u8(#x05 1 2 3 4 5))
74  (test-parse #u8(#x23) (parse-data) #u8(1 #x23)))