82 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file is a part of XRay, a function call tracing system.10//11//===----------------------------------------------------------------------===//12 13#include "xray_allocator.h"14#include "xray_buffer_queue.h"15#include "gtest/gtest.h"16 17namespace __xray {18namespace {19 20struct TestData {21 s64 First;22 s64 Second;23};24 25TEST(AllocatorTest, Construction) { Allocator<sizeof(TestData)> A(2 << 11); }26 27TEST(AllocatorTest, Allocate) {28 Allocator<sizeof(TestData)> A(2 << 11);29 auto B = A.Allocate();30 ASSERT_NE(B.Data, nullptr);31}32 33TEST(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);39}40 41struct OddSizedData {42 s64 A;43 s32 B;44};45 46TEST(AllocatorTest, AllocateBoundaries) {47 Allocator<sizeof(OddSizedData)> A(GetPageSizeCached());48 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)54 ;55 56 ASSERT_EQ(C, Expected);57}58 59TEST(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);65 {66 Allocator<sizeof(OddSizedData)> A(B.Data, B.Size);67 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)73 ;74 75 ASSERT_EQ(C, Expected);76 }77 ASSERT_EQ(BQ.releaseBuffer(B), BufferQueue::ErrorCode::Ok);78}79 80} // namespace81} // namespace __xray82