summaryrefslogtreecommitdiff
path: root/runtime/base/bit_struct_test.cc
diff options
context:
space:
mode:
author Igor Murashkin <iam@google.com> 2017-10-26 14:51:52 -0700
committer Igor Murashkin <iam@google.com> 2017-10-27 15:06:23 -0700
commitdfabcc50f13f27201fd9c0ab0def187ef5945fda (patch)
treeeee8862eebf83d94cf8fb81042666732390933aa /runtime/base/bit_struct_test.cc
parent2875173c75049777e6b63dbc9739846dd8f7d085 (diff)
base: Fix bitstruct bug with assigning fields
Given a BitStruct 'foo', where bar was a BitStructField, assignment was broken because it was using the C++ default behavior (copy the entire word). foo.bar = other_foo.bar This will now correctly use BitFieldInsert to update the 'bar' bitfield. Change-Id: I1464462ffd4df22ad14a2e43a34f74d75ac2809a
Diffstat (limited to 'runtime/base/bit_struct_test.cc')
-rw-r--r--runtime/base/bit_struct_test.cc64
1 files changed, 64 insertions, 0 deletions
diff --git a/runtime/base/bit_struct_test.cc b/runtime/base/bit_struct_test.cc
index 9fc9762054..a80d39eb91 100644
--- a/runtime/base/bit_struct_test.cc
+++ b/runtime/base/bit_struct_test.cc
@@ -255,4 +255,68 @@ TEST(BitStructs, Mixed) {
EXPECT_EQ(0b10101010101010101011111010100111u, AsUint(tst));
}
+BITSTRUCT_DEFINE_START(TestBitStruct_u8, /* size */ 8)
+ BitStructInt</*lsb*/0, /*width*/3> i3;
+ BitStructUint</*lsb*/3, /*width*/4> u4;
+
+ BitStructUint</*lsb*/0, /*width*/8> alias_all;
+BITSTRUCT_DEFINE_END(TestBitStruct_u8);
+
+TEST(BitStructs, FieldAssignment) {
+ TestBitStruct_u8 all_1s{}; // NOLINT
+ all_1s.alias_all = 0xffu;
+
+ {
+ TestBitStruct_u8 tst{}; // NOLINT
+ tst.i3 = all_1s.i3;
+
+ // Copying a single bitfield does not copy all bitfields.
+ EXPECT_EQ(0b111, tst.alias_all);
+ }
+
+ {
+ TestBitStruct_u8 tst{}; // NOLINT
+ tst.u4 = all_1s.u4;
+
+ // Copying a single bitfield does not copy all bitfields.
+ EXPECT_EQ(0b1111000, tst.alias_all);
+ }
+}
+
+BITSTRUCT_DEFINE_START(NestedStruct, /* size */ 64)
+ BitStructField<MixedSizeBitStruct, /*lsb*/0> mixed_lower;
+ BitStructField<MixedSizeBitStruct, /*lsb*/32> mixed_upper;
+
+ BitStructUint</*lsb*/0, /*width*/64> alias_all;
+BITSTRUCT_DEFINE_END(NestedStruct);
+
+TEST(BitStructs, NestedFieldAssignment) {
+ MixedSizeBitStruct mixed_all_1s{}; // NOLINT
+ mixed_all_1s.alias_all = 0xFFFFFFFFu;
+
+ {
+ NestedStruct xyz{}; // NOLINT
+
+ NestedStruct other{}; // NOLINT
+ other.mixed_upper = mixed_all_1s;
+ other.mixed_lower = mixed_all_1s;
+
+ // Copying a single bitfield does not copy all bitfields.
+ xyz.mixed_lower = other.mixed_lower;
+ EXPECT_EQ(0xFFFFFFFFu, xyz.alias_all);
+ }
+
+ {
+ NestedStruct xyz{}; // NOLINT
+
+ NestedStruct other{}; // NOLINT
+ other.mixed_upper = mixed_all_1s;
+ other.mixed_lower = mixed_all_1s;
+
+ // Copying a single bitfield does not copy all bitfields.
+ xyz.mixed_upper = other.mixed_upper;
+ EXPECT_EQ(0xFFFFFFFF00000000u, xyz.alias_all);
+ }
+}
+
} // namespace art