Skip to content

Commit 9789abb

Browse files
authored
[libc] Use anonymous namespace for test helper code (#157203)
Tests can be at top-level or inside an anonymous namespace, doesn't matter. But putting their helper code inside anonymous namespaces both makes the code compatible with compiling using -Wmissing-declarations and might let the compiler optimize the test good a bit better.
1 parent ccbcee1 commit 9789abb

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

libc/test/src/setjmp/setjmp_test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "src/setjmp/setjmp_impl.h"
1111
#include "test/UnitTest/Test.h"
1212

13+
namespace {
14+
1315
constexpr int MAX_LOOP = 123;
1416
int longjmp_called = 0;
1517

@@ -45,3 +47,5 @@ TEST(LlvmLibcSetJmpTest, SetAndJumpBackValOne) {
4547
ASSERT_EQ(longjmp_called, 1);
4648
ASSERT_EQ(val, 1);
4749
}
50+
51+
} // namespace

libc/test/src/stdio/printf_core/writer_test.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "src/__support/CPP/string_view.h"
109
#include "src/stdio/printf_core/writer.h"
1110

11+
#include "src/__support/CPP/string_view.h"
1212
#include "src/string/memory_utils/inline_memcpy.h"
13-
1413
#include "test/UnitTest/Test.h"
1514

15+
namespace {
16+
1617
using LIBC_NAMESPACE::cpp::string_view;
1718
using LIBC_NAMESPACE::printf_core::WriteBuffer;
1819
using LIBC_NAMESPACE::printf_core::WriteMode;
@@ -314,3 +315,5 @@ TEST(LlvmLibcPrintfWriterTest, NullStringWithZeroMaxLengthWithCallback) {
314315
ASSERT_EQ(writer.get_chars_written(), 12);
315316
ASSERT_STREQ("aaaDEF111456", str);
316317
}
318+
319+
} // namespace

libc/test/src/stdlib/quick_sort_test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "SortingTest.h"
1010
#include "src/stdlib/qsort_util.h"
1111

12+
namespace {
13+
1214
void quick_sort(void *array, size_t array_size, size_t elem_size,
1315
int (*compare)(const void *, const void *)) {
1416
constexpr bool USE_QUICKSORT = true;
@@ -23,3 +25,5 @@ void quick_sort(void *array, size_t array_size, size_t elem_size,
2325
}
2426

2527
LIST_SORTING_TESTS(Qsort, quick_sort);
28+
29+
} // namespace

libc/test/src/string/memchr_test.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "hdr/signal_macros.h"
109
#include "src/string/memchr.h"
11-
#include "test/UnitTest/Test.h"
10+
1211
#include <stddef.h>
1312

13+
#include "hdr/signal_macros.h"
14+
#include "test/UnitTest/Test.h"
15+
16+
namespace {
17+
1418
// A helper function that calls memchr and abstracts away the explicit cast for
1519
// readability purposes.
1620
const char *call_memchr(const void *src, int c, size_t size) {
@@ -130,3 +134,5 @@ TEST(LlvmLibcMemChrTest, CrashOnNullPtr) {
130134
}
131135

132136
#endif // defined(LIBC_ADD_NULL_CHECKS)
137+
138+
} // namespace

libc/test/src/string/memrchr_test.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "hdr/signal_macros.h"
109
#include "src/string/memrchr.h"
11-
#include "test/UnitTest/Test.h"
10+
1211
#include <stddef.h>
1312

13+
#include "hdr/signal_macros.h"
14+
#include "test/UnitTest/Test.h"
15+
16+
namespace {
17+
1418
// A helper function that calls memrchr and abstracts away the explicit cast for
1519
// readability purposes.
1620
const char *call_memrchr(const void *src, int c, size_t size) {
@@ -122,3 +126,5 @@ TEST(LlvmLibcMemRChrTest, CrashOnNullPtr) {
122126
}
123127

124128
#endif // defined(LIBC_ADD_NULL_CHECKS)
129+
130+
} // namespace

0 commit comments

Comments
 (0)