| 1 | // Copyright 2014 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <fmt/ranges.h> |
| 5 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | #include "Common/BitField.h" |
| 9 | #include "Common/CommonTypes.h" |
| 10 | #include "Common/EnumFormatter.h" |
| 11 | |
| 12 | enum class TestEnum : u64 |
| 13 | { |
| 14 | A, |
| 15 | B, |
| 16 | C, |
| 17 | D, |
| 18 | }; |
| 19 | |
| 20 | union TestUnion |
| 21 | { |
| 22 | u64 hex; |
| 23 | |
| 24 | BitField<0, 64, u64> full_u64; // spans whole storage |
| 25 | BitField<0, 64, s64> full_s64; // spans whole storage |
| 26 | |
| 27 | BitField<9, 3, u64> regular_field_unsigned; // a plain bitfield |
| 28 | BitField<9, 3, u64> regular_field_unsigned2; // Just the very same bitfield again |
| 29 | BitField<9, 3, s64> regular_field_signed; // Same bitfield, but different sign |
| 30 | |
| 31 | BitField<30, 4, s64> at_dword_boundary; // goes over the boundary of two u32 values |
| 32 | |
| 33 | BitField<15, 1, s64> signed_1bit; // allowed values: -1 and 0 |
| 34 | |
| 35 | BitField<63, 1, bool, u64> flag; |
| 36 | |
| 37 | BitField<16, 2, TestEnum> enum_1; |
| 38 | BitField<48, 2, TestEnum> enum_2; |
| 39 | }; |
| 40 | |
| 41 | // table of raw numbers to test with |
| 42 | static u64 table[] = { |
| 43 | 0x0000000000000000ull, // all zero |
| 44 | 0xffffffffffffffffull, // all one |
| 45 | 0x7fffffffffffffffull, // all one apart from the sign bit |
| 46 | 0x8000000000000000ull, // all zero apart from the sign bit |
| 47 | 0x8000000000000048ull, // regular_field = 0b1001 |
| 48 | |
| 49 | // "random" numbers |
| 50 | 0x0F7B8B1ABD9B8D3Full, |
| 51 | 0xA8B86F73FDAADD2Dull, |
| 52 | 0x1B17A557BFEB351Dull, |
| 53 | 0xE3354268B0C2395Bull, |
| 54 | }; |
| 55 | |
| 56 | // Verify that bitfields in a union have the same underlying data |
| 57 | TEST(BitField, Storage) |
| 58 | { |
| 59 | TestUnion object; |
| 60 | |
| 61 | EXPECT_EQ((void*)&object.hex, (void*)&object.regular_field_unsigned); |
| 62 | EXPECT_EQ(sizeof(TestUnion), sizeof(object.hex)); |
| 63 | EXPECT_EQ(sizeof(TestUnion), sizeof(object.full_u64)); |
| 64 | EXPECT_EQ(sizeof(TestUnion), sizeof(object.full_s64)); |
| 65 | EXPECT_EQ(sizeof(TestUnion), sizeof(object.regular_field_unsigned)); |
| 66 | EXPECT_EQ(sizeof(TestUnion), sizeof(object.regular_field_signed)); |
| 67 | EXPECT_EQ(sizeof(TestUnion), sizeof(object.at_dword_boundary)); |
| 68 | EXPECT_EQ(sizeof(TestUnion), sizeof(object.signed_1bit)); |
| 69 | EXPECT_EQ(sizeof(TestUnion), sizeof(object.flag)); |
| 70 | EXPECT_EQ(sizeof(TestUnion), sizeof(object.enum_1)); |
| 71 | EXPECT_EQ(sizeof(TestUnion), sizeof(object.enum_2)); |
| 72 | |
| 73 | // Now write some values to one field and check if this reflects properly |
| 74 | // in the others. |
| 75 | for (u64 val : table) |
| 76 | { |
| 77 | object.hex = val; |
| 78 | EXPECT_EQ(object.hex, object.full_u64); |
| 79 | EXPECT_EQ(object.regular_field_unsigned, object.regular_field_unsigned2); |
| 80 | |
| 81 | object.regular_field_unsigned = val & 0x3; |
| 82 | EXPECT_EQ(object.hex, object.full_u64); |
| 83 | EXPECT_EQ(object.regular_field_unsigned, object.regular_field_unsigned2); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | TEST(BitField, Read) |
| 88 | { |
| 89 | TestUnion object; |
| 90 | |
| 91 | for (u64 val : table) |
| 92 | { |
| 93 | object.hex = val; |
| 94 | |
| 95 | // Make sure reading/casting does not behave completely idiotic |
| 96 | EXPECT_EQ(object.full_u64, (u64)object.full_u64); |
| 97 | EXPECT_EQ(object.full_s64, (s64)object.full_s64); |
| 98 | EXPECT_EQ(object.regular_field_unsigned, (u64)object.regular_field_unsigned); |
| 99 | EXPECT_EQ(object.regular_field_unsigned2, (u64)object.regular_field_unsigned2); |
| 100 | EXPECT_EQ(object.regular_field_signed, (s64)object.regular_field_signed); |
| 101 | EXPECT_EQ(object.at_dword_boundary, (s64)object.at_dword_boundary); |
| 102 | EXPECT_EQ(object.signed_1bit, (s64)object.signed_1bit); |
| 103 | EXPECT_EQ(object.flag, (bool)object.flag); |
| 104 | EXPECT_EQ(object.enum_1, static_cast<TestEnum>(object.enum_1)); |
| 105 | EXPECT_EQ(object.enum_2, static_cast<TestEnum>(object.enum_2)); |
| 106 | |
| 107 | // Now make sure the value is indeed correct |
| 108 | EXPECT_EQ(val, object.full_u64); |
| 109 | EXPECT_EQ(*(s64*)&val, object.full_s64); |
| 110 | EXPECT_EQ((val >> 9) & 0x7, object.regular_field_unsigned); |
| 111 | EXPECT_EQ((val >> 9) & 0x7, object.regular_field_unsigned2); |
| 112 | EXPECT_EQ(((s64)(object.hex << 52)) >> 61, object.regular_field_signed); |
| 113 | EXPECT_EQ(((s64)(object.hex << 30)) >> 60, object.at_dword_boundary); |
| 114 | EXPECT_EQ(((object.hex >> 15) & 1) ? -1 : 0, object.signed_1bit); |
| 115 | EXPECT_EQ((bool)object.flag, ((object.hex >> 63) & 1)); |
| 116 | EXPECT_EQ(static_cast<TestEnum>((object.hex >> 16) & 3), object.enum_1); |
| 117 | EXPECT_EQ(static_cast<TestEnum>((object.hex >> 48) & 3), object.enum_2); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | TEST(BitField, Assignment) |
| 122 | { |
| 123 | TestUnion object; |
| 124 | |
| 125 | for (u64 val : table) |
| 126 | { |
| 127 | // Assignments with fixed values |
| 128 | object.full_u64 = val; |
| 129 | EXPECT_EQ(val, object.full_u64); |
| 130 | |
| 131 | object.full_s64 = (s64)val; |
| 132 | EXPECT_EQ(val, object.full_u64); |
| 133 | |
| 134 | object.regular_field_unsigned = val; |
| 135 | EXPECT_EQ(val & 0x7, object.regular_field_unsigned); |
| 136 | |
| 137 | object.at_dword_boundary = val; |
| 138 | EXPECT_EQ(((s64)(val << 60)) >> 60, object.at_dword_boundary); |
| 139 | |
| 140 | object.signed_1bit = val; |
| 141 | EXPECT_EQ((val & 1) ? -1 : 0, object.signed_1bit); |
| 142 | |
| 143 | object.regular_field_signed = val; |
| 144 | EXPECT_EQ(((s64)(object.hex << 61)) >> 61, object.regular_field_signed); |
| 145 | |
| 146 | // Assignment from other BitField |
| 147 | object.at_dword_boundary = object.regular_field_signed; |
| 148 | EXPECT_EQ(object.regular_field_signed, object.at_dword_boundary); |
| 149 | |
| 150 | // Assignment to field of a type with a size smaller than the underlying type |
| 151 | object.flag = (val & 2); |
| 152 | EXPECT_EQ(object.flag, (val & 2) != 0); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // Test class behavior on oddly aligned structures. |
| 157 | TEST(BitField, Alignment) |
| 158 | { |
| 159 | #pragma pack(1) |
| 160 | struct OddlyAlignedTestStruct |
| 161 | { |
| 162 | u8 padding; |
| 163 | TestUnion obj; |
| 164 | }; |
| 165 | #pragma pack() |
| 166 | |
| 167 | alignas(16) OddlyAlignedTestStruct test_struct; |
| 168 | TestUnion& object = test_struct.obj; |
| 169 | static_assert(alignof(decltype(test_struct.obj.signed_1bit)) == 1, |
| 170 | "Incorrect variable alignment"); |
| 171 | |
| 172 | for (u64 val : table) |
| 173 | { |
| 174 | // Assignments with fixed values |
| 175 | object.full_u64 = val; |
| 176 | EXPECT_EQ(val, object.full_u64); |
| 177 | |
| 178 | object.full_s64 = (s64)val; |
| 179 | EXPECT_EQ(val, object.full_u64); |
| 180 | |
| 181 | object.regular_field_unsigned = val; |
| 182 | EXPECT_EQ(val & 0x7, object.regular_field_unsigned); |
| 183 | |
| 184 | object.at_dword_boundary = val; |
| 185 | EXPECT_EQ(((s64)(val << 60)) >> 60, object.at_dword_boundary); |
| 186 | |
| 187 | object.signed_1bit = val; |
| 188 | EXPECT_EQ((val & 1) ? -1 : 0, object.signed_1bit); |
| 189 | |
| 190 | object.regular_field_signed = val; |
| 191 | EXPECT_EQ(((s64)(object.hex << 61)) >> 61, object.regular_field_signed); |
| 192 | |
| 193 | // Assignment from other BitField |
| 194 | object.at_dword_boundary = object.regular_field_signed; |
| 195 | EXPECT_EQ(object.regular_field_signed, object.at_dword_boundary); |
| 196 | |
| 197 | // Assignment to field of a type with a size smaller than the underlying type |
| 198 | object.flag = (val & 2); |
| 199 | EXPECT_EQ(object.flag, (val & 2) != 0); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | template <> |
| 204 | struct fmt::formatter<TestEnum> : EnumFormatter<TestEnum::D> |
| 205 | { |
| 206 | constexpr formatter() : EnumFormatter({"A", "B", "C", "D"}) {} |
| 207 | }; |
| 208 | |
| 209 | // Test behavior of using BitFields with fmt |
| 210 | TEST(BitField, Fmt) |
| 211 | { |
| 212 | TestUnion object; |
| 213 | |
| 214 | for (u64 val : table) |
| 215 | { |
| 216 | object.hex = val; |
| 217 | |
| 218 | // Formatting the BitField should be the same as formatting its value |
| 219 | EXPECT_EQ(fmt::to_string(object.full_u64), fmt::to_string(object.full_u64.Value())); |
| 220 | EXPECT_EQ(fmt::to_string(object.full_s64), fmt::to_string(object.full_s64.Value())); |
| 221 | EXPECT_EQ(fmt::to_string(object.regular_field_unsigned), |
| 222 | fmt::to_string(object.regular_field_unsigned.Value())); |
| 223 | EXPECT_EQ(fmt::to_string(object.regular_field_unsigned2), |
| 224 | fmt::to_string(object.regular_field_unsigned2.Value())); |
| 225 | EXPECT_EQ(fmt::to_string(object.regular_field_signed), |
| 226 | fmt::to_string(object.regular_field_signed.Value())); |
| 227 | EXPECT_EQ(fmt::to_string(object.at_dword_boundary), |
| 228 | fmt::to_string(object.at_dword_boundary.Value())); |
| 229 | EXPECT_EQ(fmt::to_string(object.signed_1bit), fmt::to_string(object.signed_1bit.Value())); |
| 230 | EXPECT_EQ(fmt::to_string(object.flag), fmt::to_string(object.flag.Value())); |
| 231 | // The custom enum formatter should be used properly. |
| 232 | EXPECT_EQ(fmt::to_string(object.enum_1), fmt::to_string(object.enum_1.Value())); |
| 233 | EXPECT_EQ(fmt::to_string(object.enum_2), fmt::to_string(object.enum_2.Value())); |
| 234 | |
| 235 | // Formatting the BitField should respect the format spec |
| 236 | EXPECT_EQ(fmt::format("{:02x}", object.full_u64), |
| 237 | fmt::format("{:02x}", object.full_u64.Value())); |
| 238 | EXPECT_EQ(fmt::format("{:02x}", object.full_s64), |
| 239 | fmt::format("{:02x}", object.full_s64.Value())); |
| 240 | EXPECT_EQ(fmt::format("{:02x}", object.regular_field_unsigned), |
| 241 | fmt::format("{:02x}", object.regular_field_unsigned.Value())); |
| 242 | EXPECT_EQ(fmt::format("{:02x}", object.regular_field_unsigned2), |
| 243 | fmt::format("{:02x}", object.regular_field_unsigned2.Value())); |
| 244 | EXPECT_EQ(fmt::format("{:02x}", object.regular_field_signed), |
| 245 | fmt::format("{:02x}", object.regular_field_signed.Value())); |
| 246 | EXPECT_EQ(fmt::format("{:02x}", object.at_dword_boundary), |
| 247 | fmt::format("{:02x}", object.at_dword_boundary.Value())); |
| 248 | EXPECT_EQ(fmt::format("{:02x}", object.signed_1bit), |
| 249 | fmt::format("{:02x}", object.signed_1bit.Value())); |
| 250 | EXPECT_EQ(fmt::format("{:02x}", object.flag), fmt::format("{:02x}", object.flag.Value())); |
| 251 | EXPECT_EQ(fmt::format("{:s}", object.enum_1), fmt::format("{:s}", object.enum_1.Value())); |
| 252 | EXPECT_EQ(fmt::format("{:s}", object.enum_2), fmt::format("{:s}", object.enum_2.Value())); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | union TestUnion2 |
| 257 | { |
| 258 | u32 hex; |
| 259 | BitField<0, 2, u32> a; |
| 260 | BitField<2, 2, u32> b; |
| 261 | BitField<4, 2, u32> c; |
| 262 | BitFieldArray<0, 2, 3, u32> arr; |
| 263 | }; |
| 264 | |
| 265 | TEST(BitFieldArray, Unsigned) |
| 266 | { |
| 267 | TestUnion2 object; |
| 268 | object.hex = 0; |
| 269 | const TestUnion2& objectc = object; |
| 270 | |
| 271 | for (u32 value : object.arr) |
| 272 | { |
| 273 | EXPECT_EQ(value, 0u); |
| 274 | } |
| 275 | |
| 276 | object.arr[0] = 2; |
| 277 | EXPECT_EQ(object.arr[0], 2u); |
| 278 | EXPECT_EQ(object.a, 2u); |
| 279 | EXPECT_EQ(object.hex, 0b00'00'10u); |
| 280 | |
| 281 | object.arr[1] = 3; |
| 282 | EXPECT_EQ(object.arr[1], 3u); |
| 283 | EXPECT_EQ(object.b, 3u); |
| 284 | EXPECT_EQ(object.hex, 0b00'11'10u); |
| 285 | |
| 286 | object.arr[2] = object.arr[1]; |
| 287 | EXPECT_EQ(object.arr[2], 3u); |
| 288 | EXPECT_EQ(object.c, 3u); |
| 289 | EXPECT_EQ(object.hex, 0b11'11'10u); |
| 290 | |
| 291 | object.arr[1] = objectc.arr[0]; |
| 292 | EXPECT_EQ(object.arr[1], 2u); |
| 293 | EXPECT_EQ(object.b, 2u); |
| 294 | EXPECT_EQ(object.hex, 0b11'10'10u); |
| 295 | |
| 296 | for (auto ref : object.arr) |
| 297 | { |
| 298 | ref = 1; |
| 299 | } |
| 300 | EXPECT_EQ(object.a, 1u); |
| 301 | EXPECT_EQ(object.b, 1u); |
| 302 | EXPECT_EQ(object.c, 1u); |
| 303 | EXPECT_EQ(object.hex, 0b01'01'01u); |
| 304 | |
| 305 | std::fill_n(object.arr.begin(), object.arr.Size(), 3); |
| 306 | EXPECT_EQ(object.arr[0], 3u); |
| 307 | EXPECT_EQ(object.arr[1], 3u); |
| 308 | EXPECT_EQ(object.arr[2], 3u); |
| 309 | EXPECT_EQ(object.hex, 0b11'11'11u); |
| 310 | |
| 311 | for (u32 i = 0; i < object.arr.Size(); i++) |
| 312 | { |
| 313 | object.arr[i] = i; |
| 314 | } |
| 315 | EXPECT_EQ(object.hex, 0b10'01'00u); |
| 316 | |
| 317 | EXPECT_EQ(objectc.arr[0], 0u); |
| 318 | EXPECT_EQ(objectc.arr[1], 1u); |
| 319 | EXPECT_EQ(objectc.arr[2], 2u); |
| 320 | |
| 321 | u32 counter = 0; |
| 322 | for (u32 value : objectc.arr) |
| 323 | { |
| 324 | EXPECT_EQ(value, counter); |
| 325 | counter++; |
| 326 | } |
| 327 | |
| 328 | EXPECT_EQ("[0, 1, 2]", fmt::format("[{}]", fmt::join(object.arr, ", "))); |
| 329 | EXPECT_EQ("[0b00, 0b01, 0b10]", fmt::format("[{:#04b}]", fmt::join(object.arr, ", "))); |
| 330 | } |
| 331 | |
| 332 | union TestUnion3 |
| 333 | { |
| 334 | s32 hex; |
| 335 | BitField<5, 2, s32> a; |
| 336 | BitField<7, 2, s32> b; |
| 337 | BitField<9, 2, s32> c; |
| 338 | BitFieldArray<5, 2, 3, s32> arr; |
| 339 | }; |
| 340 | |
| 341 | TEST(BitFieldArray, Signed) |
| 342 | { |
| 343 | TestUnion3 object; |
| 344 | object.hex = 0; |
| 345 | const TestUnion3& objectc = object; |
| 346 | |
| 347 | for (s32 value : object.arr) |
| 348 | { |
| 349 | EXPECT_EQ(value, 0); |
| 350 | } |
| 351 | |
| 352 | object.arr[0] = -2; |
| 353 | EXPECT_EQ(object.arr[0], -2); |
| 354 | EXPECT_EQ(object.a, -2); |
| 355 | EXPECT_EQ(object.hex, 0b00'00'10'00000); |
| 356 | |
| 357 | object.arr[1] = -1; |
| 358 | EXPECT_EQ(object.arr[1], -1); |
| 359 | EXPECT_EQ(object.b, -1); |
| 360 | EXPECT_EQ(object.hex, 0b00'11'10'00000); |
| 361 | |
| 362 | object.arr[2] = object.arr[1]; |
| 363 | EXPECT_EQ(object.arr[2], -1); |
| 364 | EXPECT_EQ(object.c, -1); |
| 365 | EXPECT_EQ(object.hex, 0b11'11'10'00000); |
| 366 | |
| 367 | object.arr[1] = objectc.arr[0]; |
| 368 | EXPECT_EQ(object.arr[1], -2); |
| 369 | EXPECT_EQ(object.b, -2); |
| 370 | EXPECT_EQ(object.hex, 0b11'10'10'00000); |
| 371 | |
| 372 | for (auto ref : object.arr) |
| 373 | { |
| 374 | ref = 1; |
| 375 | } |
| 376 | EXPECT_EQ(object.a, 1); |
| 377 | EXPECT_EQ(object.b, 1); |
| 378 | EXPECT_EQ(object.c, 1); |
| 379 | EXPECT_EQ(object.hex, 0b01'01'01'00000); |
| 380 | |
| 381 | std::fill_n(object.arr.begin(), object.arr.Size(), -1); |
| 382 | EXPECT_EQ(object.arr[0], -1); |
| 383 | EXPECT_EQ(object.arr[1], -1); |
| 384 | EXPECT_EQ(object.arr[2], -1); |
| 385 | EXPECT_EQ(object.hex, 0b11'11'11'00000); |
| 386 | |
| 387 | for (u32 i = 0; i < object.arr.Size(); i++) |
| 388 | { |
| 389 | object.arr[i] = i; |
| 390 | } |
| 391 | EXPECT_EQ(object.hex, 0b10'01'00'00000); |
| 392 | |
| 393 | EXPECT_EQ(objectc.arr[0], 0); |
| 394 | EXPECT_EQ(objectc.arr[1], 1); |
| 395 | EXPECT_EQ(objectc.arr[2], -2); |
| 396 | |
| 397 | u32 counter = 0; |
| 398 | for (s32 value : objectc.arr) |
| 399 | { |
| 400 | EXPECT_EQ(value, object.arr[counter++]); |
| 401 | } |
| 402 | |
| 403 | EXPECT_EQ("[0, 1, -2]", fmt::format("[{}]", fmt::join(object.arr, ", "))); |
| 404 | EXPECT_EQ("[+0b00, +0b01, -0b10]", fmt::format("[{:+#05b}]", fmt::join(object.arr, ", "))); |
| 405 | } |
| 406 | |
| 407 | union TestUnion4 |
| 408 | { |
| 409 | u64 hex; |
| 410 | BitField<30, 2, TestEnum> a; |
| 411 | BitField<32, 2, TestEnum> b; |
| 412 | BitField<34, 2, TestEnum> c; |
| 413 | BitField<36, 2, TestEnum> d; |
| 414 | BitFieldArray<30, 2, 4, TestEnum> arr; |
| 415 | }; |
| 416 | |
| 417 | TEST(BitFieldArray, Enum) |
| 418 | { |
| 419 | TestUnion4 object; |
| 420 | object.hex = 0; |
| 421 | const TestUnion4& objectc = object; |
| 422 | |
| 423 | for (TestEnum value : object.arr) |
| 424 | { |
| 425 | EXPECT_EQ(value, TestEnum::A); |
| 426 | } |
| 427 | |
| 428 | object.arr[0] = TestEnum::B; |
| 429 | EXPECT_EQ(object.arr[0], TestEnum::B); |
| 430 | EXPECT_EQ(object.a, TestEnum::B); |
| 431 | EXPECT_EQ(object.hex, 0b00'00'00'01ull << 30); |
| 432 | |
| 433 | object.arr[1] = TestEnum::C; |
| 434 | EXPECT_EQ(object.arr[1], TestEnum::C); |
| 435 | EXPECT_EQ(object.b, TestEnum::C); |
| 436 | EXPECT_EQ(object.hex, 0b00'00'10'01ull << 30); |
| 437 | |
| 438 | object.arr[2] = object.arr[1]; |
| 439 | EXPECT_EQ(object.arr[2], TestEnum::C); |
| 440 | EXPECT_EQ(object.c, TestEnum::C); |
| 441 | EXPECT_EQ(object.hex, 0b00'10'10'01ull << 30); |
| 442 | |
| 443 | object.arr[3] = objectc.arr[0]; |
| 444 | EXPECT_EQ(object.arr[3], TestEnum::B); |
| 445 | EXPECT_EQ(object.d, TestEnum::B); |
| 446 | EXPECT_EQ(object.hex, 0b01'10'10'01ull << 30); |
| 447 | |
| 448 | for (auto ref : object.arr) |
| 449 | { |
| 450 | ref = TestEnum::D; |
| 451 | } |
| 452 | EXPECT_EQ(object.a, TestEnum::D); |
| 453 | EXPECT_EQ(object.b, TestEnum::D); |
| 454 | EXPECT_EQ(object.c, TestEnum::D); |
| 455 | EXPECT_EQ(object.d, TestEnum::D); |
| 456 | EXPECT_EQ(object.hex, 0b11'11'11'11ull << 30); |
| 457 | |
| 458 | std::fill_n(object.arr.begin(), object.arr.Size(), TestEnum::C); |
| 459 | EXPECT_EQ(object.a, TestEnum::C); |
| 460 | EXPECT_EQ(object.b, TestEnum::C); |
| 461 | EXPECT_EQ(object.c, TestEnum::C); |
| 462 | EXPECT_EQ(object.d, TestEnum::C); |
| 463 | EXPECT_EQ(object.hex, 0b10'10'10'10ull << 30); |
| 464 | |
| 465 | for (u32 i = 0; i < object.arr.Size(); i++) |
| 466 | { |
| 467 | object.arr[i] = static_cast<TestEnum>(i); |
| 468 | } |
| 469 | EXPECT_EQ(object.hex, 0b11'10'01'00ull << 30); |
| 470 | |
| 471 | EXPECT_EQ(objectc.arr[0], TestEnum::A); |
| 472 | EXPECT_EQ(objectc.arr[1], TestEnum::B); |
| 473 | EXPECT_EQ(objectc.arr[2], TestEnum::C); |
| 474 | EXPECT_EQ(objectc.arr[3], TestEnum::D); |
| 475 | |
| 476 | u32 counter = 0; |
| 477 | for (TestEnum value : objectc.arr) |
| 478 | { |
| 479 | EXPECT_EQ(value, object.arr[counter++]); |
| 480 | } |
| 481 | |
| 482 | EXPECT_EQ("[A (0), B (1), C (2), D (3)]", fmt::format("[{}]", fmt::join(object.arr, ", "))); |
| 483 | EXPECT_EQ("[0x0u /* A */, 0x1u /* B */, 0x2u /* C */, 0x3u /* D */]", |
| 484 | fmt::format("[{:s}]", fmt::join(object.arr, ", "))); |
| 485 | } |
| 486 | |
| 487 | union TestUnion5 |
| 488 | { |
| 489 | u64 hex; |
| 490 | BitFieldArray<0, 5, 6, u8, u64> arr1; |
| 491 | BitFieldArray<30, 1, 4, bool, u64> arr2; |
| 492 | }; |
| 493 | |
| 494 | TEST(BitFieldArray, StorageType) |
| 495 | { |
| 496 | TestUnion5 object; |
| 497 | constexpr u64 arr2_hex_1 = 0b1010ull << 30; |
| 498 | object.hex = arr2_hex_1; |
| 499 | const TestUnion5& objectc = object; |
| 500 | |
| 501 | EXPECT_FALSE(object.arr2[0]); |
| 502 | EXPECT_TRUE(object.arr2[1]); |
| 503 | EXPECT_FALSE(object.arr2[2]); |
| 504 | EXPECT_TRUE(object.arr2[3]); |
| 505 | |
| 506 | object.arr1[0] = 0; |
| 507 | object.arr1[1] = 1; |
| 508 | object.arr1[2] = 2; |
| 509 | object.arr1[3] = 4; |
| 510 | object.arr1[4] = 8; |
| 511 | object.arr1[5] = 16; |
| 512 | constexpr u64 arr1_hex = 0b10000'01000'00100'00010'00001'00000; |
| 513 | EXPECT_EQ(object.hex, arr1_hex | arr2_hex_1); |
| 514 | |
| 515 | object.arr2[2] = object.arr2[0] = true; |
| 516 | object.arr2[3] = object.arr2[1] = false; |
| 517 | constexpr u64 arr2_hex_2 = 0b0101ull << 30; |
| 518 | EXPECT_EQ(object.hex, arr1_hex | arr2_hex_2); |
| 519 | |
| 520 | object.arr2[2] = object.arr2[1]; |
| 521 | object.arr2[3] = objectc.arr2[0]; |
| 522 | constexpr u64 arr2_hex_3 = 0b1001ull << 30; |
| 523 | EXPECT_EQ(object.hex, arr1_hex | arr2_hex_3); |
| 524 | |
| 525 | u32 counter = 0; |
| 526 | for (u8 value : object.arr1) |
| 527 | { |
| 528 | EXPECT_EQ(value, object.arr1[counter++]); |
| 529 | } |
| 530 | counter = 0; |
| 531 | for (bool value : object.arr2) |
| 532 | { |
| 533 | EXPECT_EQ(value, object.arr2[counter++]); |
| 534 | } |
| 535 | |
| 536 | counter = 0; |
| 537 | for (u8 value : objectc.arr1) |
| 538 | { |
| 539 | EXPECT_EQ(value, object.arr1[counter++]); |
| 540 | } |
| 541 | counter = 0; |
| 542 | for (bool value : objectc.arr2) |
| 543 | { |
| 544 | EXPECT_EQ(value, object.arr2[counter++]); |
| 545 | } |
| 546 | } |