File: float_ctrl_encoders_test.c

package info (click to toggle)
mysql-8.0 8.0.43-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,904 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,184; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,196; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (213 lines) | stat: -rw-r--r-- 8,570 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
 *
 * libcbor is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include <math.h>
#include "assertions.h"
#include "cbor.h"

unsigned char buffer[512];

static void test_bools(void **_CBOR_UNUSED(_state)) {
  assert_size_equal(1, cbor_encode_bool(false, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF4}), 1);
  assert_size_equal(1, cbor_encode_bool(true, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF5}), 1);
}

static void test_null(void **_CBOR_UNUSED(_state)) {
  assert_size_equal(1, cbor_encode_null(buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF6}), 1);
}

static void test_undef(void **_CBOR_UNUSED(_state)) {
  assert_size_equal(1, cbor_encode_undef(buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF7}), 1);
}

static void test_break(void **_CBOR_UNUSED(_state)) {
  assert_size_equal(1, cbor_encode_break(buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xFF}), 1);
}

/* Check that encode(decode(buffer)) = buffer for a valid half-float in the
 * buffer.*/
static void assert_half_float_codec_identity(void) {
  unsigned char secondary_buffer[3];
  struct cbor_load_result res;
  // Load and check data in buffer
  cbor_item_t *half_float = cbor_load(buffer, 3, &res);
  assert_size_equal(res.error.code, CBOR_ERR_NONE);
  assert_true(cbor_isa_float_ctrl(half_float));
  assert_true(cbor_is_float(half_float));
  assert_size_equal(cbor_float_get_width(half_float), CBOR_FLOAT_16);
  // Encode again and check equality
  assert_size_equal(3, cbor_encode_half(cbor_float_get_float2(half_float),
                                        secondary_buffer, 3));
  assert_memory_equal(buffer, secondary_buffer, 3);
  cbor_decref(&half_float);
}

static void test_half(void **_CBOR_UNUSED(_state)) {
  assert_size_equal(3, cbor_encode_half(1.5f, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x3E, 0x00}), 3);
  assert_half_float_codec_identity();

  assert_size_equal(3, cbor_encode_half(-0.0f, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x80, 0x00}), 3);
  assert_half_float_codec_identity();

  assert_size_equal(3, cbor_encode_half(0.0f, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x00, 0x00}), 3);
  assert_half_float_codec_identity();

  assert_size_equal(3, cbor_encode_half(65504.0f, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x7B, 0xFF}), 3);
  assert_half_float_codec_identity();

  assert_size_equal(3, cbor_encode_half(0.00006103515625f, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x04, 0x00}), 3);
  assert_half_float_codec_identity();

  assert_size_equal(3, cbor_encode_half(-4.0f, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0xC4, 0x00}), 3);
  assert_half_float_codec_identity();

  /* Smallest representable value */
  assert_size_equal(3, cbor_encode_half(5.960464477539063e-8f, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x00, 0x01}), 3);
  assert_half_float_codec_identity();

  /* Smaller than the smallest, approximate magnitude representation */
  assert_size_equal(3, cbor_encode_half(5.960464477539062e-8f, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x00, 0x01}), 3);
  assert_half_float_codec_identity();

  assert_size_equal(3, cbor_encode_half(4.172325134277344e-7f, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x00, 0x07}), 3);
  assert_half_float_codec_identity();

  assert_size_equal(3, cbor_encode_half(6.097555160522461e-5f, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x03, 0xff}), 3);
  assert_half_float_codec_identity();

  assert_size_equal(3, cbor_encode_half(6.100535392761231e-5f, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x04, 0x00}), 3);
  assert_half_float_codec_identity();

  /* Smaller than the smallest and even the magnitude cannot be represented,
     round off to zero */
  assert_size_equal(3, cbor_encode_half(1e-25f, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x00, 0x00}), 3);
  assert_half_float_codec_identity();

  assert_size_equal(3, cbor_encode_half(1.1920928955078125e-7, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x00, 0x02}), 3);
  assert_half_float_codec_identity();

  assert_size_equal(3, cbor_encode_half(-1.1920928955078124e-7, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x80, 0x02}), 3);
  assert_half_float_codec_identity();

  assert_size_equal(3, cbor_encode_half(INFINITY, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x7C, 0x00}), 3);
  assert_half_float_codec_identity();
}

static void test_half_special(void **_CBOR_UNUSED(_state)) {
  assert_size_equal(3, cbor_encode_half(NAN, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x7E, 0x00}), 3);
  assert_half_float_codec_identity();

  // We discard all information bits in half-float NaNs. This is
  // not required for the core CBOR protocol (it is only a suggestion in
  // Section 3.9).
  // See https://github.com/PJK/libcbor/issues/215
  assert_size_equal(3, cbor_encode_half(nanf("2"), buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x7E, 0x00}), 3);
  assert_half_float_codec_identity();
}

static void test_half_infinity(void **_CBOR_UNUSED(_state)) {
  assert_size_equal(3, cbor_encode_half(INFINITY, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x7C, 0x00}), 3);
  assert_half_float_codec_identity();

  assert_size_equal(3, cbor_encode_half(-INFINITY, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0xFC, 0x00}), 3);
  assert_half_float_codec_identity();
}

static void test_float(void **_CBOR_UNUSED(_state)) {
  assert_size_equal(5, cbor_encode_single(3.4028234663852886e+38, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xFA, 0x7F, 0x7F, 0xFF, 0xFF}),
                      5);

  assert_size_equal(5, cbor_encode_single(NAN, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xFA, 0x7F, 0xC0, 0x00, 0x00}),
                      5);

#ifndef _WIN32
  // TODO: https://github.com/PJK/libcbor/issues/271
  assert_size_equal(5, cbor_encode_single(nanf("3"), buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xFA, 0x7F, 0xC0, 0x00, 0x03}),
                      5);
#endif

  assert_size_equal(5, cbor_encode_single(strtof("Inf", NULL), buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xFA, 0x7F, 0x80, 0x00, 0x00}),
                      5);

  assert_size_equal(5, cbor_encode_single(strtof("-Inf", NULL), buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0xFA, 0xFF, 0x80, 0x00, 0x00}),
                      5);
}

static void test_double(void **_CBOR_UNUSED(_state)) {
  assert_size_equal(9, cbor_encode_double(1.0e+300, buffer, 512));
  assert_memory_equal(
      buffer,
      ((unsigned char[]){0xFB, 0x7E, 0x37, 0xE4, 0x3C, 0x88, 0x00, 0x75, 0x9C}),
      9);

  assert_size_equal(9, cbor_encode_double(nan(""), buffer, 512));
  assert_memory_equal(
      buffer,
      ((unsigned char[]){0xFB, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
      9);

#ifndef _WIN32
  // TODO: https://github.com/PJK/libcbor/issues/271
  assert_size_equal(9, cbor_encode_double(nan("3"), buffer, 512));
  assert_memory_equal(
      buffer,
      ((unsigned char[]){0xFB, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}),
      9);
#endif

  assert_size_equal(9, cbor_encode_double(strtod("Inf", NULL), buffer, 512));
  assert_memory_equal(
      buffer,
      ((unsigned char[]){0xFB, 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
      9);

  assert_size_equal(9, cbor_encode_double(strtod("-Inf", NULL), buffer, 512));
  assert_memory_equal(
      buffer,
      ((unsigned char[]){0xFB, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
      9);
}

int main(void) {
  const struct CMUnitTest tests[] = {
      cmocka_unit_test(test_bools),         cmocka_unit_test(test_null),
      cmocka_unit_test(test_undef),         cmocka_unit_test(test_break),
      cmocka_unit_test(test_half),          cmocka_unit_test(test_float),
      cmocka_unit_test(test_double),        cmocka_unit_test(test_half_special),
      cmocka_unit_test(test_half_infinity),
  };
  return cmocka_run_group_tests(tests, NULL, NULL);
}