@@ -413,30 +413,37 @@ TEST(memory_buffer_test, exception_in_deallocate) {
413
413
EXPECT_CALL (alloc, deallocate (&mem2[0 ], 2 * size));
414
414
}
415
415
416
- class smol_allocator : public std ::allocator<char > {
416
+ template <typename Allocator, size_t MaxSize>
417
+ class max_size_allocator : public Allocator {
417
418
public:
418
- using size_type = unsigned char ;
419
-
420
- auto allocate (size_t n) -> value_type* {
421
- if (n > fmt::detail::max_value<size_type> ())
419
+ using typename Allocator::value_type ;
420
+ size_t max_size () const noexcept { return MaxSize; }
421
+ value_type* allocate (size_t n) {
422
+ if (n > max_size ()) {
422
423
throw std::length_error (" size > max_size" );
423
- return std::allocator<char >::allocate (n);
424
+ }
425
+ return std::allocator_traits<Allocator>::allocate (
426
+ *static_cast <Allocator*>(this ), n);
424
427
}
425
428
void deallocate (value_type* p, size_t n) {
426
- std::allocator<char >::deallocate (p, n);
429
+ std::allocator_traits<Allocator>::deallocate (*static_cast <Allocator*>(this ),
430
+ p, n);
427
431
}
428
432
};
429
433
430
434
TEST (memory_buffer_test, max_size_allocator) {
431
- basic_memory_buffer<char , 10 , smol_allocator> buffer;
432
- buffer.resize (200 );
433
- // new_capacity = 200 + 200/2 = 300 > 256
434
- buffer.resize (255 ); // Shouldn't throw.
435
+ // 160 = 128 + 32
436
+ using test_allocator = max_size_allocator<std::allocator<char >, 160 >;
437
+ basic_memory_buffer<char , 10 , test_allocator> buffer;
438
+ buffer.resize (128 );
439
+ // new_capacity = 128 + 128/2 = 192 > 160
440
+ buffer.resize (160 ); // Shouldn't throw.
435
441
}
436
442
437
443
TEST (memory_buffer_test, max_size_allocator_overflow) {
438
- basic_memory_buffer<char , 10 , smol_allocator> buffer;
439
- EXPECT_THROW (buffer.resize (256 ), std::exception );
444
+ using test_allocator = max_size_allocator<std::allocator<char >, 160 >;
445
+ basic_memory_buffer<char , 10 , test_allocator> buffer;
446
+ EXPECT_THROW (buffer.resize (161 ), std::exception );
440
447
}
441
448
442
449
TEST (format_test, exception_from_lib) {
@@ -2152,7 +2159,7 @@ TEST(format_int_test, format_int) {
2152
2159
EXPECT_EQ (fmt::format_int (42ul ).str (), " 42" );
2153
2160
EXPECT_EQ (fmt::format_int (-42l ).str (), " -42" );
2154
2161
EXPECT_EQ (fmt::format_int (42ull ).str (), " 42" );
2155
- EXPECT_EQ (fmt::format_int (-42ll ).str (), " -42" );\
2162
+ EXPECT_EQ (fmt::format_int (-42ll ).str (), " -42" );
2156
2163
EXPECT_EQ (fmt::format_int (max_value<int64_t >()).str (),
2157
2164
std::to_string (max_value<int64_t >()));
2158
2165
}
0 commit comments