1 //===-- allocator_test.cc -------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of XRay, a function call tracing system.
12 //===----------------------------------------------------------------------===//
14 #include "xray_allocator.h"
15 #include "xray_buffer_queue.h"
16 #include "gtest/gtest.h"
26 TEST(AllocatorTest, Construction) { Allocator<sizeof(TestData)> A(2 << 11); }
28 TEST(AllocatorTest, Allocate) {
29 Allocator<sizeof(TestData)> A(2 << 11);
30 auto B = A.Allocate();
31 ASSERT_NE(B.Data, nullptr);
34 TEST(AllocatorTest, OverAllocate) {
35 Allocator<sizeof(TestData)> A(sizeof(TestData));
36 auto B1 = A.Allocate();
37 ASSERT_NE(B1.Data, nullptr);
38 auto B2 = A.Allocate();
39 ASSERT_EQ(B2.Data, nullptr);
47 TEST(AllocatorTest, AllocateBoundaries) {
48 Allocator<sizeof(OddSizedData)> A(GetPageSizeCached());
50 // Keep allocating until we hit a nullptr block.
53 GetPageSizeCached() / RoundUpTo(sizeof(OddSizedData), kCacheLineSize);
54 for (auto B = A.Allocate(); B.Data != nullptr; B = A.Allocate(), ++C)
57 ASSERT_EQ(C, Expected);
60 TEST(AllocatorTest, AllocateFromNonOwned) {
62 BufferQueue BQ(GetPageSizeCached(), 10, Success);
64 BufferQueue::Buffer B;
65 ASSERT_EQ(BQ.getBuffer(B), BufferQueue::ErrorCode::Ok);
67 Allocator<sizeof(OddSizedData)> A(B.Data, B.Size);
69 // Keep allocating until we hit a nullptr block.
72 GetPageSizeCached() / RoundUpTo(sizeof(OddSizedData), kCacheLineSize);
73 for (auto B = A.Allocate(); B.Data != nullptr; B = A.Allocate(), ++C)
76 ASSERT_EQ(C, Expected);
78 ASSERT_EQ(BQ.releaseBuffer(B), BufferQueue::ErrorCode::Ok);