File: enum.ml

package info (click to toggle)
ocaml-cstruct 6.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 968 kB
  • sloc: ml: 3,676; ansic: 56; javascript: 21; makefile: 18
file content (95 lines) | stat: -rw-r--r-- 2,145 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(*
 * Copyright (c) 2016 Hannes Mehnert <hannes@mehnert.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *)

[%%cenum
type foo64 =
  | ONE64
  | TWO64
  | THREE64
  [@@uint64_t] [@@sexp]
]

[%%cenum
type bar64 =
  | ONE64
  | TWO64 [@id 0xfffffffffffffffeL]
  | THREE64
  [@@uint64_t] [@@sexp]
]

[%%cenum
type foo32 =
  | ONE32
  | TWO32 [@id 0xfffffffel]
  | THREE32
  [@@uint32_t]
]

[%%cenum
type bar16 =
  | ONE [@id 1]
  | TWO
  | FOUR [@id 4]
  | FIVE
  [@@uint16_t]
]

[%%cenum
type foo16 =
  | ONE16
  | TWO16
  | THREE16
  [@@uint16_t]
]

[%%cenum
type foo8 =
  | ONE8
  | TWO8
  | THREE8
  [@@uint8_t]
]

[%%cenum
type reversed =
  | ONE_R [@id 2]
  | TWO_R [@id 1]
  [@@uint8_t]
]

let tests () =
  ignore(int_to_foo64 2L);
  ignore(int_to_foo32 1l);
  ignore(int_to_foo16 1);
  ignore(int_to_foo8 1);
  ignore(foo64_to_int ONE64);
  ignore(foo32_to_int ONE32);
  ignore(foo16_to_int ONE16);
  ignore(foo8_to_int ONE8);
  assert(bar16_to_int FOUR = 4);
  assert(bar16_to_int FIVE = 5);
  assert(foo32_to_int TWO32   = 0xfffffffel);
  assert(foo32_to_int THREE32 = 0xffffffffl);
  assert(int_to_foo32 0xfffffffel = Some (TWO32));
  assert(int_to_foo32 0xffffffffl = Some (THREE32));
  assert(string_to_foo16 "ONE16" = Some ONE16);
  assert(foo8_to_string ONE8 = "ONE8");
  assert(compare_foo8 ONE8 TWO8 = -1);
  assert(compare_foo8 TWO8 ONE8 = 1);
  assert(compare_foo8 TWO8 TWO8 = 0);
  assert(compare_reversed ONE_R TWO_R = 1)

let () = tests ()