@@ -47,6 +47,9 @@ enum PreviousState { kInline, kMicroRep, kOwned, kUnowned, kString, kAlias };
47
47
static constexpr auto kUnownedPayload =
48
48
MicroString::MakeUnownedPayload (" 0123456789" );
49
49
50
+ static constexpr auto kInlineInput =
51
+ absl::string_view (" 0123456789" ).substr(0 , MicroString::kInlineCapacity );
52
+
50
53
class MicroStringPrevTest
51
54
: public testing::TestWithParam<std::tuple<bool , PreviousState>> {
52
55
protected:
@@ -157,7 +160,7 @@ INSTANTIATE_TEST_SUITE_P(MicroStringTransitionTest, MicroStringPrevTest,
157
160
158
161
TEST (MicroStringTest, InlineIsEnabledWhenExpected) {
159
162
#if defined(ABSL_IS_LITTLE_ENDIAN)
160
- constexpr bool kExpectInline = sizeof ( uintptr_t ) >= 8 ;
163
+ constexpr bool kExpectInline = true ;
161
164
#else
162
165
constexpr bool kExpectInline = false ;
163
166
#endif
@@ -266,21 +269,24 @@ TEST(MicroStringTest, SupportsExpectedInputTypes) {
266
269
267
270
template <int N>
268
271
void TestExtraCapacity (int expected_sizeof) {
269
- EXPECT_EQ (sizeof (MicroStringExtra<N>), expected_sizeof);
270
- EXPECT_EQ (MicroStringExtra<N>::kInlineCapacity , expected_sizeof - 1 );
272
+ EXPECT_EQ (sizeof (MicroStringExtra<N>), expected_sizeof) << N ;
273
+ EXPECT_EQ (MicroStringExtra<N>::kInlineCapacity , expected_sizeof - 1 ) << N ;
271
274
}
272
275
273
276
TEST (MicroStringTest, ExtraRequestedInlineSpace) {
274
277
if (!MicroString::kHasInlineRep ) {
275
278
GTEST_SKIP () << " Inline is not active" ;
276
279
}
277
- TestExtraCapacity<0 >(8 );
278
- TestExtraCapacity<1 >(8 );
279
- TestExtraCapacity<7 >(8 );
280
- TestExtraCapacity<8 >(16 );
281
- TestExtraCapacity<15 >(16 );
282
- TestExtraCapacity<16 >(24 );
283
- TestExtraCapacity<23 >(24 );
280
+ // We write in terms of steps to support 64 and 32 bits.
281
+ static constexpr size_t kStep = alignof (MicroString);
282
+ TestExtraCapacity<0 * kStep + 0 >(1 * kStep );
283
+ TestExtraCapacity<0 * kStep + 1 >(1 * kStep );
284
+ TestExtraCapacity<1 * kStep - 1 >(1 * kStep );
285
+ TestExtraCapacity<1 * kStep + 0 >(2 * kStep );
286
+ TestExtraCapacity<2 * kStep - 1 >(2 * kStep );
287
+ TestExtraCapacity<2 * kStep + 0 >(3 * kStep );
288
+ TestExtraCapacity<3 * kStep - 1 >(3 * kStep );
289
+ TestExtraCapacity<3 * kStep + 0 >(4 * kStep );
284
290
}
285
291
286
292
TEST (MicroStringTest, CapacityIsRoundedUpOnArena) {
@@ -364,7 +370,7 @@ TEST_P(MicroStringPrevTest, SetInline) {
364
370
GTEST_SKIP () << " Inline is not active" ;
365
371
}
366
372
367
- const absl::string_view input ( " ABCD " ) ;
373
+ const absl::string_view input = kInlineInput ;
368
374
const size_t used = arena_space_used ();
369
375
const size_t self_used = str_.SpaceUsedExcludingSelfLong ();
370
376
str_.Set (input, arena ());
@@ -414,7 +420,7 @@ TEST_P(MicroStringPrevTest, SetAliasSmall) {
414
420
if (!MicroString::kHasInlineRep ) {
415
421
GTEST_SKIP () << " Inline is not active" ;
416
422
}
417
- const absl::string_view input ( " ABC " ) ;
423
+ const absl::string_view input = kInlineInput ;
418
424
419
425
const size_t used = arena_space_used ();
420
426
const size_t self_used = str_.SpaceUsedExcludingSelfLong ();
@@ -430,8 +436,11 @@ TEST_P(MicroStringPrevTest, SetAliasSmall) {
430
436
}
431
437
EXPECT_EQ (out, input);
432
438
433
- // We should not need to allocate memory here in any case.
434
- ExpectMemoryUsed (used, false , self_used);
439
+ // In 32-bit mode, we will use memory that is not rounded to the arena
440
+ // alignment because sizeof(LargeRep)==12. Avoid using `ExpectMemoryUsed`
441
+ // because it expects it.
442
+ EXPECT_EQ (0 , arena_space_used () - used);
443
+ EXPECT_EQ (self_used, str_.SpaceUsedExcludingSelfLong ());
435
444
}
436
445
437
446
TEST_P (MicroStringPrevTest, SetAliasLarge) {
@@ -895,7 +904,8 @@ TEST(MicroStringExtraTest, SetStringUsesInlineSpace) {
895
904
const size_t used_in_string = StringSpaceUsedExcludingSelfLong (large);
896
905
str.Set (std::move (large), &arena);
897
906
// This one is too big, so we move the whole std::string.
898
- EXPECT_EQ (kLargeRepSize + sizeof (std::string), arena.SpaceUsed () - used);
907
+ EXPECT_EQ (ArenaAlignDefault::Ceil (kLargeRepSize + sizeof (std::string)),
908
+ arena.SpaceUsed () - used);
899
909
EXPECT_EQ (kLargeRepSize + sizeof (std::string) + used_in_string,
900
910
str.SpaceUsedExcludingSelfLong ());
901
911
}
0 commit comments