sanitizer_allocator_test.cc revision 864f5131db7ccd3fc8344dc2bcdebf66c03a900e
1//===-- sanitizer_allocator_test.cc ---------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11//
12//===----------------------------------------------------------------------===//
13#include "sanitizer_common/sanitizer_common.h"
14#include "gtest/gtest.h"
15#include <stdlib.h>
16
17namespace __sanitizer {
18
19TEST(Allocator, Basic) {
20  char *p = (char*)InternalAlloc(10);
21  EXPECT_NE(p, (char*)0);
22  char *p2 = (char*)InternalAlloc(20);
23  EXPECT_NE(p2, (char*)0);
24  EXPECT_NE(p2, p);
25  InternalFree(p);
26  InternalFree(p2);
27}
28
29TEST(Allocator, Stress) {
30  const int kCount = 1000;
31  char *ptrs[kCount];
32  unsigned rnd = 42;
33  for (int i = 0; i < kCount; i++) {
34    uptr sz = rand_r(&rnd) % 1000;
35    char *p = (char*)InternalAlloc(sz);
36    EXPECT_NE(p, (char*)0);
37    ptrs[i] = p;
38  }
39  for (int i = 0; i < kCount; i++) {
40    InternalFree(ptrs[i]);
41  }
42}
43
44TEST(Allocator, ScopedBuffer) {
45  const int kSize = 512;
46  {
47    InternalScopedBuffer<int> int_buf(kSize);
48    EXPECT_EQ(sizeof(int) * kSize, int_buf.size());  // NOLINT
49  }
50  InternalScopedBuffer<char> char_buf(kSize);
51  EXPECT_EQ(sizeof(char) * kSize, char_buf.size());  // NOLINT
52  memset(char_buf.data(), 'c', kSize);
53  for (int i = 0; i < kSize; i++) {
54    EXPECT_EQ('c', char_buf[i]);
55  }
56}
57
58}  // namespace __sanitizer
59