@@ -249,3 +249,92 @@ TEMPLATE_TEST_CASE("bit_size", "[bit]", std::uint8_t, std::uint16_t,
249
249
static_assert (stdx::bit_size<TestType>() ==
250
250
std::numeric_limits<std::make_unsigned_t <TestType>>::digits);
251
251
}
252
+
253
+ TEST_CASE (" bit_unpack 64 -> 2x32" , " [bit]" ) {
254
+ auto const [a, b] =
255
+ stdx::bit_unpack<std::uint32_t >(std::uint64_t {0x1234'5678'9abc'def0 });
256
+ CHECK (a == 0x1234'5678 );
257
+ CHECK (b == 0x9abc'def0 );
258
+ }
259
+
260
+ TEST_CASE (" bit_unpack 32 -> 2x16" , " [bit]" ) {
261
+ auto const [a, b] =
262
+ stdx::bit_unpack<std::uint16_t >(std::uint32_t {0x1234'5678 });
263
+ CHECK (a == 0x1234 );
264
+ CHECK (b == 0x5678 );
265
+ }
266
+
267
+ TEST_CASE (" bit_unpack 64 -> 4x16" , " [bit]" ) {
268
+ auto const [a, b, c, d] =
269
+ stdx::bit_unpack<std::uint16_t >(std::uint64_t {0x1234'5678'9abc'def0 });
270
+ CHECK (a == 0x1234 );
271
+ CHECK (b == 0x5678 );
272
+ CHECK (c == 0x9abc );
273
+ CHECK (d == 0xdef0 );
274
+ }
275
+
276
+ TEST_CASE (" bit_unpack 16 -> 2x8" , " [bit]" ) {
277
+ auto const [a, b] = stdx::bit_unpack<std::uint8_t >(std::uint16_t {0x1234 });
278
+ CHECK (a == 0x12 );
279
+ CHECK (b == 0x34 );
280
+ }
281
+
282
+ TEST_CASE (" bit_unpack 32 -> 4x8" , " [bit]" ) {
283
+ auto const [a, b, c, d] =
284
+ stdx::bit_unpack<std::uint8_t >(std::uint32_t {0x1234'5678 });
285
+ CHECK (a == 0x12 );
286
+ CHECK (b == 0x34 );
287
+ CHECK (c == 0x56 );
288
+ CHECK (d == 0x78 );
289
+ }
290
+
291
+ TEST_CASE (" bit_unpack 64 -> 8x8" , " [bit]" ) {
292
+ auto const [a, b, c, d, e, f, g, h] =
293
+ stdx::bit_unpack<std::uint8_t >(std::uint64_t {0x1234'5678'9abc'def0 });
294
+ CHECK (a == 0x12 );
295
+ CHECK (b == 0x34 );
296
+ CHECK (c == 0x56 );
297
+ CHECK (d == 0x78 );
298
+ CHECK (e == 0x9a );
299
+ CHECK (f == 0xbc );
300
+ CHECK (g == 0xde );
301
+ CHECK (h == 0xf0 );
302
+ }
303
+
304
+ TEST_CASE (" bit_pack/unpack round trip 16 <-> 8" , " [bit]" ) {
305
+ constexpr auto x = stdx::bit_pack<std::uint16_t >(0x12 , 0x34 );
306
+ auto const [a, b] = stdx::bit_unpack<std::uint8_t >(x);
307
+ CHECK (stdx::bit_pack<std::uint16_t >(a, b) == x);
308
+ }
309
+
310
+ TEST_CASE (" bit_pack/unpack round trip 32 <-> 8" , " [bit]" ) {
311
+ constexpr auto x = stdx::bit_pack<std::uint32_t >(0x12 , 0x34 , 0x56 , 0x78 );
312
+ auto const [a, b, c, d] = stdx::bit_unpack<std::uint8_t >(x);
313
+ CHECK (stdx::bit_pack<std::uint32_t >(a, b, c, d) == x);
314
+ }
315
+
316
+ TEST_CASE (" bit_pack/unpack round trip 64 <-> 8" , " [bit]" ) {
317
+ constexpr auto x = stdx::bit_pack<std::uint64_t >(0x12 , 0x34 , 0x56 , 0x78 ,
318
+ 0x9a , 0xbc , 0xde , 0xf0 );
319
+ auto const [a, b, c, d, e, f, g, h] = stdx::bit_unpack<std::uint8_t >(x);
320
+ CHECK (stdx::bit_pack<std::uint64_t >(a, b, c, d, e, f, g, h) == x);
321
+ }
322
+
323
+ TEST_CASE (" bit_pack/unpack round trip 32 <-> 16" , " [bit]" ) {
324
+ constexpr auto x = stdx::bit_pack<std::uint32_t >(0x1234 , 0x5678 );
325
+ auto const [a, b] = stdx::bit_unpack<std::uint16_t >(x);
326
+ CHECK (stdx::bit_pack<std::uint32_t >(a, b) == x);
327
+ }
328
+
329
+ TEST_CASE (" bit_pack/unpack round trip 64 <-> 16" , " [bit]" ) {
330
+ constexpr auto x =
331
+ stdx::bit_pack<std::uint64_t >(0x1234 , 0x5678 , 0x9abc , 0xdef0 );
332
+ auto const [a, b, c, d] = stdx::bit_unpack<std::uint16_t >(x);
333
+ CHECK (stdx::bit_pack<std::uint64_t >(a, b, c, d) == x);
334
+ }
335
+
336
+ TEST_CASE (" bit_pack/unpack round trip 64 <-> 32" , " [bit]" ) {
337
+ constexpr auto x = stdx::bit_pack<std::uint64_t >(0x1234'5678 , 0x9abcdef0 );
338
+ auto const [a, b] = stdx::bit_unpack<std::uint32_t >(x);
339
+ CHECK (stdx::bit_pack<std::uint64_t >(a, b) == x);
340
+ }
0 commit comments