summaryrefslogtreecommitdiff
path: root/Source/UnitTests/Common/BitFieldTest.cpp
blob: 24ef6bd90cf9a3a256c8e1044860c354d367d278 (plain)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// Copyright 2014 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <fmt/ranges.h>

#include <gtest/gtest.h>

#include "Common/BitField.h"
#include "Common/CommonTypes.h"
#include "Common/EnumFormatter.h"

enum class TestEnum : u64
{
  A,
  B,
  C,
  D,
};

union TestUnion
{
  u64 hex;

  BitField<0, 64, u64> full_u64;  // spans whole storage
  BitField<0, 64, s64> full_s64;  // spans whole storage

  BitField<9, 3, u64> regular_field_unsigned;   // a plain bitfield
  BitField<9, 3, u64> regular_field_unsigned2;  // Just the very same bitfield again
  BitField<9, 3, s64> regular_field_signed;     // Same bitfield, but different sign

  BitField<30, 4, s64> at_dword_boundary;  // goes over the boundary of two u32 values

  BitField<15, 1, s64> signed_1bit;  // allowed values: -1 and 0

  BitField<63, 1, bool, u64> flag;

  BitField<16, 2, TestEnum> enum_1;
  BitField<48, 2, TestEnum> enum_2;
};

// table of raw numbers to test with
static u64 table[] = {
    0x0000000000000000ull,  // all zero
    0xffffffffffffffffull,  // all one
    0x7fffffffffffffffull,  // all one apart from the sign bit
    0x8000000000000000ull,  // all zero apart from the sign bit
    0x8000000000000048ull,  // regular_field = 0b1001

    // "random" numbers
    0x0F7B8B1ABD9B8D3Full,
    0xA8B86F73FDAADD2Dull,
    0x1B17A557BFEB351Dull,
    0xE3354268B0C2395Bull,
};

// Verify that bitfields in a union have the same underlying data
TEST(BitField, Storage)
{
  TestUnion object;

  EXPECT_EQ((void*)&object.hex, (void*)&object.regular_field_unsigned);
  EXPECT_EQ(sizeof(TestUnion), sizeof(object.hex));
  EXPECT_EQ(sizeof(TestUnion), sizeof(object.full_u64));
  EXPECT_EQ(sizeof(TestUnion), sizeof(object.full_s64));
  EXPECT_EQ(sizeof(TestUnion), sizeof(object.regular_field_unsigned));
  EXPECT_EQ(sizeof(TestUnion), sizeof(object.regular_field_signed));
  EXPECT_EQ(sizeof(TestUnion), sizeof(object.at_dword_boundary));
  EXPECT_EQ(sizeof(TestUnion), sizeof(object.signed_1bit));
  EXPECT_EQ(sizeof(TestUnion), sizeof(object.flag));
  EXPECT_EQ(sizeof(TestUnion), sizeof(object.enum_1));
  EXPECT_EQ(sizeof(TestUnion), sizeof(object.enum_2));

  // Now write some values to one field and check if this reflects properly
  // in the others.
  for (u64 val : table)
  {
    object.hex = val;
    EXPECT_EQ(object.hex, object.full_u64);
    EXPECT_EQ(object.regular_field_unsigned, object.regular_field_unsigned2);

    object.regular_field_unsigned = val & 0x3;
    EXPECT_EQ(object.hex, object.full_u64);
    EXPECT_EQ(object.regular_field_unsigned, object.regular_field_unsigned2);
  }
}

TEST(BitField, Read)
{
  TestUnion object;

  for (u64 val : table)
  {
    object.hex = val;

    // Make sure reading/casting does not behave completely idiotic
    EXPECT_EQ(object.full_u64, (u64)object.full_u64);
    EXPECT_EQ(object.full_s64, (s64)object.full_s64);
    EXPECT_EQ(object.regular_field_unsigned, (u64)object.regular_field_unsigned);
    EXPECT_EQ(object.regular_field_unsigned2, (u64)object.regular_field_unsigned2);
    EXPECT_EQ(object.regular_field_signed, (s64)object.regular_field_signed);
    EXPECT_EQ(object.at_dword_boundary, (s64)object.at_dword_boundary);
    EXPECT_EQ(object.signed_1bit, (s64)object.signed_1bit);
    EXPECT_EQ(object.flag, (bool)object.flag);
    EXPECT_EQ(object.enum_1, static_cast<TestEnum>(object.enum_1));
    EXPECT_EQ(object.enum_2, static_cast<TestEnum>(object.enum_2));

    // Now make sure the value is indeed correct
    EXPECT_EQ(val, object.full_u64);
    EXPECT_EQ(*(s64*)&val, object.full_s64);
    EXPECT_EQ((val >> 9) & 0x7, object.regular_field_unsigned);
    EXPECT_EQ((val >> 9) & 0x7, object.regular_field_unsigned2);
    EXPECT_EQ(((s64)(object.hex << 52)) >> 61, object.regular_field_signed);
    EXPECT_EQ(((s64)(object.hex << 30)) >> 60, object.at_dword_boundary);
    EXPECT_EQ(((object.hex >> 15) & 1) ? -1 : 0, object.signed_1bit);
    EXPECT_EQ((bool)object.flag, ((object.hex >> 63) & 1));
    EXPECT_EQ(static_cast<TestEnum>((object.hex >> 16) & 3), object.enum_1);
    EXPECT_EQ(static_cast<TestEnum>((object.hex >> 48) & 3), object.enum_2);
  }
}

TEST(BitField, Assignment)
{
  TestUnion object;

  for (u64 val : table)
  {
    // Assignments with fixed values
    object.full_u64 = val;
    EXPECT_EQ(val, object.full_u64);

    object.full_s64 = (s64)val;
    EXPECT_EQ(val, object.full_u64);

    object.regular_field_unsigned = val;
    EXPECT_EQ(val & 0x7, object.regular_field_unsigned);

    object.at_dword_boundary = val;
    EXPECT_EQ(((s64)(val << 60)) >> 60, object.at_dword_boundary);

    object.signed_1bit = val;
    EXPECT_EQ((val & 1) ? -1 : 0, object.signed_1bit);

    object.regular_field_signed = val;
    EXPECT_EQ(((s64)(object.hex << 61)) >> 61, object.regular_field_signed);

    // Assignment from other BitField
    object.at_dword_boundary = object.regular_field_signed;
    EXPECT_EQ(object.regular_field_signed, object.at_dword_boundary);

    // Assignment to field of a type with a size smaller than the underlying type
    object.flag = (val & 2);
    EXPECT_EQ(object.flag, (val & 2) != 0);
  }
}

// Test class behavior on oddly aligned structures.
TEST(BitField, Alignment)
{
#pragma pack(1)
  struct OddlyAlignedTestStruct
  {
    u8 padding;
    TestUnion obj;
  };
#pragma pack()

  alignas(16) OddlyAlignedTestStruct test_struct;
  TestUnion& object = test_struct.obj;
  static_assert(alignof(decltype(test_struct.obj.signed_1bit)) == 1,
                "Incorrect variable alignment");

  for (u64 val : table)
  {
    // Assignments with fixed values
    object.full_u64 = val;
    EXPECT_EQ(val, object.full_u64);

    object.full_s64 = (s64)val;
    EXPECT_EQ(val, object.full_u64);

    object.regular_field_unsigned = val;
    EXPECT_EQ(val & 0x7, object.regular_field_unsigned);

    object.at_dword_boundary = val;
    EXPECT_EQ(((s64)(val << 60)) >> 60, object.at_dword_boundary);

    object.signed_1bit = val;
    EXPECT_EQ((val & 1) ? -1 : 0, object.signed_1bit);

    object.regular_field_signed = val;
    EXPECT_EQ(((s64)(object.hex << 61)) >> 61, object.regular_field_signed);

    // Assignment from other BitField
    object.at_dword_boundary = object.regular_field_signed;
    EXPECT_EQ(object.regular_field_signed, object.at_dword_boundary);

    // Assignment to field of a type with a size smaller than the underlying type
    object.flag = (val & 2);
    EXPECT_EQ(object.flag, (val & 2) != 0);
  }
}

template <>
struct fmt::formatter<TestEnum> : EnumFormatter<TestEnum::D>
{
  constexpr formatter() : EnumFormatter({"A", "B", "C", "D"}) {}
};

// Test behavior of using BitFields with fmt
TEST(BitField, Fmt)
{
  TestUnion object;

  for (u64 val : table)
  {
    object.hex = val;

    // Formatting the BitField should be the same as formatting its value
    EXPECT_EQ(fmt::to_string(object.full_u64), fmt::to_string(object.full_u64.Value()));
    EXPECT_EQ(fmt::to_string(object.full_s64), fmt::to_string(object.full_s64.Value()));
    EXPECT_EQ(fmt::to_string(object.regular_field_unsigned),
              fmt::to_string(object.regular_field_unsigned.Value()));
    EXPECT_EQ(fmt::to_string(object.regular_field_unsigned2),
              fmt::to_string(object.regular_field_unsigned2.Value()));
    EXPECT_EQ(fmt::to_string(object.regular_field_signed),
              fmt::to_string(object.regular_field_signed.Value()));
    EXPECT_EQ(fmt::to_string(object.at_dword_boundary),
              fmt::to_string(object.at_dword_boundary.Value()));
    EXPECT_EQ(fmt::to_string(object.signed_1bit), fmt::to_string(object.signed_1bit.Value()));
    EXPECT_EQ(fmt::to_string(object.flag), fmt::to_string(object.flag.Value()));
    // The custom enum formatter should be used properly.
    EXPECT_EQ(fmt::to_string(object.enum_1), fmt::to_string(object.enum_1.Value()));
    EXPECT_EQ(fmt::to_string(object.enum_2), fmt::to_string(object.enum_2.Value()));

    // Formatting the BitField should respect the format spec
    EXPECT_EQ(fmt::format("{:02x}", object.full_u64),
              fmt::format("{:02x}", object.full_u64.Value()));
    EXPECT_EQ(fmt::format("{:02x}", object.full_s64),
              fmt::format("{:02x}", object.full_s64.Value()));
    EXPECT_EQ(fmt::format("{:02x}", object.regular_field_unsigned),
              fmt::format("{:02x}", object.regular_field_unsigned.Value()));
    EXPECT_EQ(fmt::format("{:02x}", object.regular_field_unsigned2),
              fmt::format("{:02x}", object.regular_field_unsigned2.Value()));
    EXPECT_EQ(fmt::format("{:02x}", object.regular_field_signed),
              fmt::format("{:02x}", object.regular_field_signed.Value()));
    EXPECT_EQ(fmt::format("{:02x}", object.at_dword_boundary),
              fmt::format("{:02x}", object.at_dword_boundary.Value()));
    EXPECT_EQ(fmt::format("{:02x}", object.signed_1bit),
              fmt::format("{:02x}", object.signed_1bit.Value()));
    EXPECT_EQ(fmt::format("{:02x}", object.flag), fmt::format("{:02x}", object.flag.Value()));
    EXPECT_EQ(fmt::format("{:s}", object.enum_1), fmt::format("{:s}", object.enum_1.Value()));
    EXPECT_EQ(fmt::format("{:s}", object.enum_2), fmt::format("{:s}", object.enum_2.Value()));
  }
}

union TestUnion2
{
  u32 hex;
  BitField<0, 2, u32> a;
  BitField<2, 2, u32> b;
  BitField<4, 2, u32> c;
  BitFieldArray<0, 2, 3, u32> arr;
};

TEST(BitFieldArray, Unsigned)
{
  TestUnion2 object;
  object.hex = 0;
  const TestUnion2& objectc = object;

  for (u32 value : object.arr)
  {
    EXPECT_EQ(value, 0u);
  }

  object.arr[0] = 2;
  EXPECT_EQ(object.arr[0], 2u);
  EXPECT_EQ(object.a, 2u);
  EXPECT_EQ(object.hex, 0b00'00'10u);

  object.arr[1] = 3;
  EXPECT_EQ(object.arr[1], 3u);
  EXPECT_EQ(object.b, 3u);
  EXPECT_EQ(object.hex, 0b00'11'10u);

  object.arr[2] = object.arr[1];
  EXPECT_EQ(object.arr[2], 3u);
  EXPECT_EQ(object.c, 3u);
  EXPECT_EQ(object.hex, 0b11'11'10u);

  object.arr[1] = objectc.arr[0];
  EXPECT_EQ(object.arr[1], 2u);
  EXPECT_EQ(object.b, 2u);
  EXPECT_EQ(object.hex, 0b11'10'10u);

  for (auto ref : object.arr)
  {
    ref = 1;
  }
  EXPECT_EQ(object.a, 1u);
  EXPECT_EQ(object.b, 1u);
  EXPECT_EQ(object.c, 1u);
  EXPECT_EQ(object.hex, 0b01'01'01u);

  std::fill_n(object.arr.begin(), object.arr.Size(), 3);
  EXPECT_EQ(object.arr[0], 3u);
  EXPECT_EQ(object.arr[1], 3u);
  EXPECT_EQ(object.arr[2], 3u);
  EXPECT_EQ(object.hex, 0b11'11'11u);

  for (u32 i = 0; i < object.arr.Size(); i++)
  {
    object.arr[i] = i;
  }
  EXPECT_EQ(object.hex, 0b10'01'00u);

  EXPECT_EQ(objectc.arr[0], 0u);
  EXPECT_EQ(objectc.arr[1], 1u);
  EXPECT_EQ(objectc.arr[2], 2u);

  u32 counter = 0;
  for (u32 value : objectc.arr)
  {
    EXPECT_EQ(value, counter);
    counter++;
  }

  EXPECT_EQ("[0, 1, 2]", fmt::format("[{}]", fmt::join(object.arr, ", ")));
  EXPECT_EQ("[0b00, 0b01, 0b10]", fmt::format("[{:#04b}]", fmt::join(object.arr, ", ")));
}

union TestUnion3
{
  s32 hex;
  BitField<5, 2, s32> a;
  BitField<7, 2, s32> b;
  BitField<9, 2, s32> c;
  BitFieldArray<5, 2, 3, s32> arr;
};

TEST(BitFieldArray, Signed)
{
  TestUnion3 object;
  object.hex = 0;
  const TestUnion3& objectc = object;

  for (s32 value : object.arr)
  {
    EXPECT_EQ(value, 0);
  }

  object.arr[0] = -2;
  EXPECT_EQ(object.arr[0], -2);
  EXPECT_EQ(object.a, -2);
  EXPECT_EQ(object.hex, 0b00'00'10'00000);

  object.arr[1] = -1;
  EXPECT_EQ(object.arr[1], -1);
  EXPECT_EQ(object.b, -1);
  EXPECT_EQ(object.hex, 0b00'11'10'00000);

  object.arr[2] = object.arr[1];
  EXPECT_EQ(object.arr[2], -1);
  EXPECT_EQ(object.c, -1);
  EXPECT_EQ(object.hex, 0b11'11'10'00000);

  object.arr[1] = objectc.arr[0];
  EXPECT_EQ(object.arr[1], -2);
  EXPECT_EQ(object.b, -2);
  EXPECT_EQ(object.hex, 0b11'10'10'00000);

  for (auto ref : object.arr)
  {
    ref = 1;
  }
  EXPECT_EQ(object.a, 1);
  EXPECT_EQ(object.b, 1);
  EXPECT_EQ(object.c, 1);
  EXPECT_EQ(object.hex, 0b01'01'01'00000);

  std::fill_n(object.arr.begin(), object.arr.Size(), -1);
  EXPECT_EQ(object.arr[0], -1);
  EXPECT_EQ(object.arr[1], -1);
  EXPECT_EQ(object.arr[2], -1);
  EXPECT_EQ(object.hex, 0b11'11'11'00000);

  for (u32 i = 0; i < object.arr.Size(); i++)
  {
    object.arr[i] = i;
  }
  EXPECT_EQ(object.hex, 0b10'01'00'00000);

  EXPECT_EQ(objectc.arr[0], 0);
  EXPECT_EQ(objectc.arr[1], 1);
  EXPECT_EQ(objectc.arr[2], -2);

  u32 counter = 0;
  for (s32 value : objectc.arr)
  {
    EXPECT_EQ(value, object.arr[counter++]);
  }

  EXPECT_EQ("[0, 1, -2]", fmt::format("[{}]", fmt::join(object.arr, ", ")));
  EXPECT_EQ("[+0b00, +0b01, -0b10]", fmt::format("[{:+#05b}]", fmt::join(object.arr, ", ")));
}

union TestUnion4
{
  u64 hex;
  BitField<30, 2, TestEnum> a;
  BitField<32, 2, TestEnum> b;
  BitField<34, 2, TestEnum> c;
  BitField<36, 2, TestEnum> d;
  BitFieldArray<30, 2, 4, TestEnum> arr;
};

TEST(BitFieldArray, Enum)
{
  TestUnion4 object;
  object.hex = 0;
  const TestUnion4& objectc = object;

  for (TestEnum value : object.arr)
  {
    EXPECT_EQ(value, TestEnum::A);
  }

  object.arr[0] = TestEnum::B;
  EXPECT_EQ(object.arr[0], TestEnum::B);
  EXPECT_EQ(object.a, TestEnum::B);
  EXPECT_EQ(object.hex, 0b00'00'00'01ull << 30);

  object.arr[1] = TestEnum::C;
  EXPECT_EQ(object.arr[1], TestEnum::C);
  EXPECT_EQ(object.b, TestEnum::C);
  EXPECT_EQ(object.hex, 0b00'00'10'01ull << 30);

  object.arr[2] = object.arr[1];
  EXPECT_EQ(object.arr[2], TestEnum::C);
  EXPECT_EQ(object.c, TestEnum::C);
  EXPECT_EQ(object.hex, 0b00'10'10'01ull << 30);

  object.arr[3] = objectc.arr[0];
  EXPECT_EQ(object.arr[3], TestEnum::B);
  EXPECT_EQ(object.d, TestEnum::B);
  EXPECT_EQ(object.hex, 0b01'10'10'01ull << 30);

  for (auto ref : object.arr)
  {
    ref = TestEnum::D;
  }
  EXPECT_EQ(object.a, TestEnum::D);
  EXPECT_EQ(object.b, TestEnum::D);
  EXPECT_EQ(object.c, TestEnum::D);
  EXPECT_EQ(object.d, TestEnum::D);
  EXPECT_EQ(object.hex, 0b11'11'11'11ull << 30);

  std::fill_n(object.arr.begin(), object.arr.Size(), TestEnum::C);
  EXPECT_EQ(object.a, TestEnum::C);
  EXPECT_EQ(object.b, TestEnum::C);
  EXPECT_EQ(object.c, TestEnum::C);
  EXPECT_EQ(object.d, TestEnum::C);
  EXPECT_EQ(object.hex, 0b10'10'10'10ull << 30);

  for (u32 i = 0; i < object.arr.Size(); i++)
  {
    object.arr[i] = static_cast<TestEnum>(i);
  }
  EXPECT_EQ(object.hex, 0b11'10'01'00ull << 30);

  EXPECT_EQ(objectc.arr[0], TestEnum::A);
  EXPECT_EQ(objectc.arr[1], TestEnum::B);
  EXPECT_EQ(objectc.arr[2], TestEnum::C);
  EXPECT_EQ(objectc.arr[3], TestEnum::D);

  u32 counter = 0;
  for (TestEnum value : objectc.arr)
  {
    EXPECT_EQ(value, object.arr[counter++]);
  }

  EXPECT_EQ("[A (0), B (1), C (2), D (3)]", fmt::format("[{}]", fmt::join(object.arr, ", ")));
  EXPECT_EQ("[0x0u /* A */, 0x1u /* B */, 0x2u /* C */, 0x3u /* D */]",
            fmt::format("[{:s}]", fmt::join(object.arr, ", ")));
}

union TestUnion5
{
  u64 hex;
  BitFieldArray<0, 5, 6, u8, u64> arr1;
  BitFieldArray<30, 1, 4, bool, u64> arr2;
};

TEST(BitFieldArray, StorageType)
{
  TestUnion5 object;
  const u64 arr2_hex_1 = 0b1010ull << 30;
  object.hex = arr2_hex_1;
  const TestUnion5& objectc = object;

  EXPECT_FALSE(object.arr2[0]);
  EXPECT_TRUE(object.arr2[1]);
  EXPECT_FALSE(object.arr2[2]);
  EXPECT_TRUE(object.arr2[3]);

  object.arr1[0] = 0;
  object.arr1[1] = 1;
  object.arr1[2] = 2;
  object.arr1[3] = 4;
  object.arr1[4] = 8;
  object.arr1[5] = 16;
  const u64 arr1_hex = 0b10000'01000'00100'00010'00001'00000;
  EXPECT_EQ(object.hex, arr1_hex | arr2_hex_1);

  object.arr2[2] = object.arr2[0] = true;
  object.arr2[3] = object.arr2[1] = false;
  const u64 arr2_hex_2 = 0b0101ull << 30;
  EXPECT_EQ(object.hex, arr1_hex | arr2_hex_2);

  object.arr2[2] = object.arr2[1];
  object.arr2[3] = objectc.arr2[0];
  const u64 arr2_hex_3 = 0b1001ull << 30;
  EXPECT_EQ(object.hex, arr1_hex | arr2_hex_3);

  u32 counter = 0;
  for (u8 value : object.arr1)
  {
    EXPECT_EQ(value, object.arr1[counter++]);
  }
  counter = 0;
  for (bool value : object.arr2)
  {
    EXPECT_EQ(value, object.arr2[counter++]);
  }

  counter = 0;
  for (u8 value : objectc.arr1)
  {
    EXPECT_EQ(value, object.arr1[counter++]);
  }
  counter = 0;
  for (bool value : objectc.arr2)
  {
    EXPECT_EQ(value, object.arr2[counter++]);
  }
}