• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MinaProtocol / mina / 2903

15 Nov 2024 01:59PM UTC coverage: 36.723% (-25.0%) from 61.682%
2903

Pull #16342

buildkite

dkijania
Merge branch 'dkijania/remove_publish_job_from_pr_comp' into dkijania/remove_publish_job_from_pr_dev
Pull Request #16342: [DEV] Publish debians only on nightly and stable

15 of 40 new or added lines in 14 files covered. (37.5%)

15175 existing lines in 340 files now uncovered.

24554 of 66863 relevant lines covered (36.72%)

20704.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

49.18
/src/lib/hex/hex.ml
1
open Core_kernel
23✔
2

3
module Digit = struct
4
  (* A number between 0 and 15 *)
5
  type t =
6
    | H0
7
    | H1
8
    | H2
9
    | H3
10
    | H4
11
    | H5
12
    | H6
13
    | H7
14
    | H8
15
    | H9
16
    | H10
17
    | H11
18
    | H12
19
    | H13
20
    | H14
21
    | H15
22

23
  let of_char_exn c =
24
    match Char.lowercase c with
1,862,848✔
25
    | '0' ->
1,491,119✔
26
        H0
27
    | '1' ->
22,586✔
28
        H1
29
    | '2' ->
27,867✔
30
        H2
31
    | '3' ->
16,454✔
32
        H3
33
    | '4' ->
33,181✔
34
        H4
35
    | '5' ->
42,683✔
36
        H5
37
    | '6' ->
17,316✔
38
        H6
39
    | '7' ->
12,599✔
40
        H7
41
    | '8' ->
17,799✔
42
        H8
43
    | '9' ->
31,635✔
44
        H9
45
    | 'a' ->
43,673✔
46
        H10
47
    | 'b' ->
14,464✔
48
        H11
49
    | 'c' ->
22,712✔
50
        H12
51
    | 'd' ->
14,688✔
52
        H13
53
    | 'e' ->
14,354✔
54
        H14
55
    | 'f' ->
39,718✔
56
        H15
57
    | _ ->
×
58
        failwithf "bad hex digit %c" c ()
59

60
  let to_int = function
61
    | H0 ->
1,491,119✔
62
        0
63
    | H1 ->
22,586✔
64
        1
65
    | H2 ->
27,867✔
66
        2
67
    | H3 ->
16,454✔
68
        3
69
    | H4 ->
33,181✔
70
        4
71
    | H5 ->
42,683✔
72
        5
73
    | H6 ->
17,316✔
74
        6
75
    | H7 ->
12,599✔
76
        7
77
    | H8 ->
17,799✔
78
        8
79
    | H9 ->
31,635✔
80
        9
81
    | H10 ->
43,673✔
82
        10
83
    | H11 ->
14,464✔
84
        11
85
    | H12 ->
22,712✔
86
        12
87
    | H13 ->
14,688✔
88
        13
89
    | H14 ->
14,354✔
90
        14
91
    | H15 ->
39,718✔
92
        15
93
end
94

95
let hex_char_of_int_exn = function
96
  | 0 ->
1,949,064✔
97
      '0'
98
  | 1 ->
336,549✔
99
      '1'
100
  | 2 ->
293,208✔
101
      '2'
102
  | 3 ->
360,267✔
103
      '3'
104
  | 4 ->
290,165✔
105
      '4'
106
  | 5 ->
298,264✔
107
      '5'
108
  | 6 ->
270,848✔
109
      '6'
110
  | 7 ->
324,946✔
111
      '7'
112
  | 8 ->
253,378✔
113
      '8'
114
  | 9 ->
274,462✔
115
      '9'
116
  | 10 ->
314,015✔
117
      'a'
118
  | 11 ->
299,060✔
119
      'b'
120
  | 12 ->
268,820✔
121
      'c'
122
  | 13 ->
278,583✔
123
      'd'
124
  | 14 ->
262,837✔
125
      'e'
126
  | 15 ->
288,222✔
127
      'f'
128
  | d ->
×
129
      failwithf "bad hex digit %d" d ()
130

131
module Sequence_be = struct
132
  type t = Digit.t array
133

UNCOV
134
  let decode ?(pos = 0) s =
×
UNCOV
135
    let n = String.length s - pos in
×
UNCOV
136
    Array.init n ~f:(fun i -> Digit.of_char_exn s.[pos + i])
×
137

138
  let to_bytes_like ~init (t : t) =
UNCOV
139
    let n = Array.length t in
×
UNCOV
140
    let k = n / 2 in
×
UNCOV
141
    assert (n = k + k) ;
×
142
    init k ~f:(fun i ->
UNCOV
143
        Char.of_int_exn
×
UNCOV
144
          ((16 * Digit.to_int t.(2 * i)) + Digit.to_int t.((2 * i) + 1)) )
×
145

146
  let to_string = to_bytes_like ~init:String.init
147

148
  let to_bytes = to_bytes_like ~init:Bytes.init
149

150
  let to_bigstring = to_bytes_like ~init:Bigstring.init
151
end
152

UNCOV
153
let decode ?(reverse = false) ?(pos = 0) ~init t =
×
154
  let n = String.length t - pos in
29,107✔
155
  let k = n / 2 in
156
  assert (n = k + k) ;
29,107✔
157
  let h j = Digit.(to_int (of_char_exn t.[pos + j])) in
1,862,848✔
158
  init k ~f:(fun i ->
UNCOV
159
      let i = if reverse then k - 1 - i else i in
×
160
      Char.of_int_exn ((16 * h (2 * i)) + h ((2 * i) + 1)) )
931,424✔
161

UNCOV
162
let encode ?(reverse = false) t =
×
163
  let n = String.length t in
99,417✔
164
  String.init (2 * n) ~f:(fun i ->
99,417✔
165
      let c =
6,362,688✔
166
        let byte = i / 2 in
UNCOV
167
        Char.to_int t.[if reverse then n - 1 - byte else byte]
×
168
      in
169
      let c = if i mod 2 = 0 then (* hi *)
170
                c lsr 4 else (* lo *)
3,181,344✔
171
                          c in
3,181,344✔
172
      hex_char_of_int_exn (c land 15) )
173

174
let%test_unit "decode" =
UNCOV
175
  let t = String.init 100 ~f:(fun _ -> Char.of_int_exn (Random.int 256)) in
×
UNCOV
176
  let h = encode t in
×
UNCOV
177
  assert (String.equal t (decode ~init:String.init h)) ;
×
UNCOV
178
  assert (
×
UNCOV
179
    String.equal t
×
UNCOV
180
      (decode ~reverse:true ~init:String.init (encode ~reverse:true t)) ) ;
×
UNCOV
181
  assert (String.equal t Sequence_be.(to_string (decode h)))
×
182

183
(* TODO: Better deduplicate the hex coding between these two implementations #5711 *)
184
module Safe = struct
185
  (** to_hex : {0x0-0xff}* -> [A-F0-9]* *)
186
  let to_hex (data : string) : string =
UNCOV
187
    String.to_list data
×
UNCOV
188
    |> List.map ~f:(fun c ->
×
UNCOV
189
           let charify u4 =
×
UNCOV
190
             match u4 with
×
UNCOV
191
             | x when x <= 9 && x >= 0 ->
×
UNCOV
192
                 Char.(of_int_exn @@ (x + to_int '0'))
×
UNCOV
193
             | x when x <= 15 && x >= 10 ->
×
UNCOV
194
                 Char.(of_int_exn @@ (x - 10 + to_int 'A'))
×
195
             | _ ->
×
196
                 failwith "Unexpected u4 has only 4bits of information"
197
           in
UNCOV
198
           let high = charify @@ ((Char.to_int c land 0xF0) lsr 4) in
×
UNCOV
199
           let lo = charify (Char.to_int c land 0x0F) in
×
UNCOV
200
           String.of_char_list [ high; lo ] )
×
201
    |> String.concat
202

203
  let%test_unit "to_hex sane" =
UNCOV
204
    let start = "a" in
×
205
    let hexified = to_hex start in
UNCOV
206
    let expected = "61" in
×
UNCOV
207
    if String.equal expected hexified then ()
×
208
    else
209
      failwithf "start: %s ; hexified : %s ; expected: %s" start hexified
×
210
        expected ()
211

212
  (** of_hex : [a-fA-F0-9]* -> {0x0-0xff}* option *)
213
  let of_hex (hex : string) : string option =
UNCOV
214
    let to_u4 c =
×
UNCOV
215
      let open Char in
×
UNCOV
216
      assert (is_alphanum c) ;
×
217
      match c with
UNCOV
218
      | _ when is_digit c ->
×
UNCOV
219
          to_int c - to_int '0'
×
UNCOV
220
      | _ when is_uppercase c ->
×
UNCOV
221
          to_int c - to_int 'A' + 10
×
222
      | _ (* when is_alpha *) ->
×
223
          to_int c - to_int 'a' + 10
×
224
    in
UNCOV
225
    String.to_list hex |> List.chunks_of ~length:2
×
UNCOV
226
    |> List.fold_result ~init:[] ~f:(fun acc chunk ->
×
UNCOV
227
           match chunk with
×
UNCOV
228
           | [ a; b ] when Char.is_alphanum a && Char.is_alphanum b ->
×
UNCOV
229
               Or_error.return
×
UNCOV
230
               @@ (Char.((to_u4 a lsl 4) lor to_u4 b |> of_int_exn) :: acc)
×
231
           | _ ->
×
232
               Or_error.error_string "invalid hex" )
UNCOV
233
    |> Or_error.ok
×
UNCOV
234
    |> Option.map ~f:(Fn.compose String.of_char_list List.rev)
×
235

236
  let%test_unit "partial isomorphism" =
UNCOV
237
    Quickcheck.test ~sexp_of:[%sexp_of: string] ~examples:[ "\243"; "abc" ]
×
UNCOV
238
      Quickcheck.Generator.(map (list char) ~f:String.of_char_list)
×
239
      ~f:(fun s ->
UNCOV
240
        let hexified = to_hex s in
×
UNCOV
241
        let actual = Option.value_exn (of_hex hexified) in
×
UNCOV
242
        let expected = s in
×
UNCOV
243
        if String.equal actual expected then ()
×
244
        else
245
          failwithf
×
246
            !"expected: %s ; hexified: %s ; actual: %s"
247
            expected hexified actual () )
248
end
46✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc