aboutsummaryrefslogtreecommitdiff
path: root/lib/sanitizer_common/tests/sanitizer_allocator_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sanitizer_common/tests/sanitizer_allocator_test.cc')
-rw-r--r--lib/sanitizer_common/tests/sanitizer_allocator_test.cc56
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/sanitizer_common/tests/sanitizer_allocator_test.cc b/lib/sanitizer_common/tests/sanitizer_allocator_test.cc
new file mode 100644
index 000000000000..d6c7f56dc143
--- /dev/null
+++ b/lib/sanitizer_common/tests/sanitizer_allocator_test.cc
@@ -0,0 +1,56 @@
+//===-- sanitizer_allocator_test.cc ---------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
+//
+//===----------------------------------------------------------------------===//
+#include "sanitizer_common/sanitizer_common.h"
+#include "gtest/gtest.h"
+#include <stdlib.h>
+
+namespace __sanitizer {
+
+TEST(Allocator, Basic) {
+ char *p = (char*)InternalAlloc(10);
+ EXPECT_NE(p, (char*)0);
+ char *p2 = (char*)InternalAlloc(20);
+ EXPECT_NE(p2, (char*)0);
+ EXPECT_NE(p2, p);
+ for (int i = 0; i < 10; i++) {
+ p[i] = 42;
+ EXPECT_EQ(p, InternalAllocBlock(p + i));
+ }
+ for (int i = 0; i < 20; i++) {
+ ((char*)p2)[i] = 42;
+ EXPECT_EQ(p2, InternalAllocBlock(p2 + i));
+ }
+ InternalFree(p);
+ InternalFree(p2);
+}
+
+TEST(Allocator, Stress) {
+ const int kCount = 1000;
+ char *ptrs[kCount];
+ unsigned rnd = 42;
+ for (int i = 0; i < kCount; i++) {
+ uptr sz = rand_r(&rnd) % 1000;
+ char *p = (char*)InternalAlloc(sz);
+ EXPECT_NE(p, (char*)0);
+ for (uptr j = 0; j < sz; j++) {
+ p[j] = 42;
+ EXPECT_EQ(p, InternalAllocBlock(p + j));
+ }
+ ptrs[i] = p;
+ }
+ for (int i = 0; i < kCount; i++) {
+ InternalFree(ptrs[i]);
+ }
+}
+
+} // namespace __sanitizer