Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / xray / tests / unit / allocator_test.cpp
blob25557632402049c256167303110d4180d294bab3
1 //===-- allocator_test.cpp ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of XRay, a function call tracing system.
11 //===----------------------------------------------------------------------===//
13 #include "xray_allocator.h"
14 #include "xray_buffer_queue.h"
15 #include "gtest/gtest.h"
17 namespace __xray {
18 namespace {
20 struct TestData {
21 s64 First;
22 s64 Second;
25 TEST(AllocatorTest, Construction) { Allocator<sizeof(TestData)> A(2 << 11); }
27 TEST(AllocatorTest, Allocate) {
28 Allocator<sizeof(TestData)> A(2 << 11);
29 auto B = A.Allocate();
30 ASSERT_NE(B.Data, nullptr);
33 TEST(AllocatorTest, OverAllocate) {
34 Allocator<sizeof(TestData)> A(sizeof(TestData));
35 auto B1 = A.Allocate();
36 ASSERT_NE(B1.Data, nullptr);
37 auto B2 = A.Allocate();
38 ASSERT_EQ(B2.Data, nullptr);
41 struct OddSizedData {
42 s64 A;
43 s32 B;
46 TEST(AllocatorTest, AllocateBoundaries) {
47 Allocator<sizeof(OddSizedData)> A(GetPageSizeCached());
49 // Keep allocating until we hit a nullptr block.
50 unsigned C = 0;
51 auto Expected =
52 GetPageSizeCached() / RoundUpTo(sizeof(OddSizedData), kCacheLineSize);
53 for (auto B = A.Allocate(); B.Data != nullptr; B = A.Allocate(), ++C)
56 ASSERT_EQ(C, Expected);
59 TEST(AllocatorTest, AllocateFromNonOwned) {
60 bool Success = false;
61 BufferQueue BQ(GetPageSizeCached(), 10, Success);
62 ASSERT_TRUE(Success);
63 BufferQueue::Buffer B;
64 ASSERT_EQ(BQ.getBuffer(B), BufferQueue::ErrorCode::Ok);
66 Allocator<sizeof(OddSizedData)> A(B.Data, B.Size);
68 // Keep allocating until we hit a nullptr block.
69 unsigned C = 0;
70 auto Expected =
71 GetPageSizeCached() / RoundUpTo(sizeof(OddSizedData), kCacheLineSize);
72 for (auto B = A.Allocate(); B.Data != nullptr; B = A.Allocate(), ++C)
75 ASSERT_EQ(C, Expected);
77 ASSERT_EQ(BQ.releaseBuffer(B), BufferQueue::ErrorCode::Ok);
80 } // namespace
81 } // namespace __xray