[libcxx-commits] [libcxx] [libcxx][string] Test: default constructed allocators can be unequal (PR #195839)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jun 25 05:01:04 PDT 2026


================
@@ -36,7 +36,7 @@ TEST_CONSTEXPR_CXX20 void test(It first, It last) {
     ++it;
     ++i;
   }
-  assert(s2.get_allocator() == Alloc());
----------------
ldionne wrote:

It seems to me the test was trying to check that we use `Allocator()` as the default argument in

```
template<class InputIterator>
basic_string(InputIterator begin, InputIterator end, const Allocator& a = Allocator());
```

I think we should refactor this test a bit further. Specifically, drop this assertion from `void test(It first, It last)`. Then, test the actual default argument by adding something around line 101 (in `TEST_CONSTEXPR_CXX20 bool test()`) where we pass an allocator whose default value we control, and check that. Something like:

```
static int foo;
struct AllocatorWeControlDefaultConstructionOf {
  AllocatorWeControlDefaultConstructionOf() : value(foo++) { }
  int value;
};

{ // check the default allocator argument
  auto iter1, iter2 = ...;
  foo = 42;
  std::basic_string<char, char_traits<char>, AllocatorWeControlDefaultConstructionOf> s(iter1, iter2);
  assert(s.get_allocator().value == 42);
}
```

That would ensure we truly used the right default argument. I think this approach can and should be used in other tests similarly.

Then `ASSERT_CONTAINER_ALLOCATOR_EQUALS_DEFAULT` becomes unneeded.

https://github.com/llvm/llvm-project/pull/195839


More information about the libcxx-commits mailing list