From 78b34e592100750ad0c648740eb3c1a06f2721ac Mon Sep 17 00:00:00 2001 From: Derek Bruening Date: Tue, 31 May 2016 14:44:49 +0000 Subject: [esan] Add circular buffer data structure Summary: Adds a new class, CircularBuffer, for holding a wrap-around fixed-size sequence of a primitive data type. This will be used initially by the working set tool. Adds a unit test for CircularBuffer, including infrastructure support to include esan headers and to link with the esan library by pretending to want the working set tool. Reviewers: aizatsky, filcab Subscribers: vitalybuka, zhaoqin, kcc, eugenis, llvm-commits, kubabrecka Differential Revision: http://reviews.llvm.org/D20579 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@271286 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/esan/Unit/circular_buffer.cpp | 61 ++++++++++++++++++++++++++++++++++++++ test/esan/lit.cfg | 6 ++++ 2 files changed, 67 insertions(+) create mode 100644 test/esan/Unit/circular_buffer.cpp (limited to 'test/esan') diff --git a/test/esan/Unit/circular_buffer.cpp b/test/esan/Unit/circular_buffer.cpp new file mode 100644 index 000000000..a788418b7 --- /dev/null +++ b/test/esan/Unit/circular_buffer.cpp @@ -0,0 +1,61 @@ +// RUN: %clangxx_unit -O0 %s -o %t 2>&1 +// RUN: %run %t 2>&1 | FileCheck %s + +#include "esan/esan_circular_buffer.h" +#include "sanitizer_common/sanitizer_placement_new.h" +#include +#include + +static const int TestBufCapacity = 4; + +// The buffer should have a capacity of TestBufCapacity. +void testBuffer(__esan::CircularBuffer *Buf) { + assert(Buf->size() == 0); + assert(Buf->empty()); + + Buf->push_back(1); + assert(Buf->back() == 1); + assert((*Buf)[0] == 1); + assert(Buf->size() == 1); + assert(!Buf->empty()); + + Buf->push_back(2); + Buf->push_back(3); + Buf->push_back(4); + Buf->push_back(5); + assert((*Buf)[0] == 2); + assert(Buf->size() == 4); + + Buf->pop_back(); + assert((*Buf)[0] == 2); + assert(Buf->size() == 3); + + Buf->pop_back(); + Buf->pop_back(); + assert((*Buf)[0] == 2); + assert(Buf->size() == 1); + assert(!Buf->empty()); + + Buf->pop_back(); + assert(Buf->empty()); +} + +int main() +{ + // Test initialize/free. + __esan::CircularBuffer GlobalBuf; + GlobalBuf.initialize(TestBufCapacity); + testBuffer(&GlobalBuf); + GlobalBuf.free(); + + // Test constructor/free. + __esan::CircularBuffer *LocalBuf; + static char placeholder[sizeof(*LocalBuf)]; + LocalBuf = new(placeholder) __esan::CircularBuffer(TestBufCapacity); + testBuffer(LocalBuf); + LocalBuf->free(); + + fprintf(stderr, "All checks passed.\n"); + // CHECK: All checks passed. + return 0; +} diff --git a/test/esan/lit.cfg b/test/esan/lit.cfg index cc7492c88..9eb296d90 100644 --- a/test/esan/lit.cfg +++ b/test/esan/lit.cfg @@ -14,6 +14,10 @@ base_cxxflags = config.cxx_mode_flags + base_cflags frag_cflags = (["-fsanitize=efficiency-cache-frag"] + base_cflags) wset_cflags = (["-fsanitize=efficiency-working-set"] + base_cflags) +esan_incdir = config.test_source_root + "/../../lib" +unit_cxxflags = (["-I%s" % esan_incdir, "-std=c++11", + # We need to link with the esan runtime. + "-fsanitize=efficiency-working-set"] + base_cxxflags) def build_invocation(compile_flags): return " " + " ".join([config.clang] + compile_flags) + " " @@ -22,6 +26,8 @@ config.substitutions.append( ("%clang_esan_frag ", build_invocation(frag_cflags)) ) config.substitutions.append( ("%clang_esan_wset ", build_invocation(wset_cflags)) ) +config.substitutions.append( ("%clangxx_unit", + build_invocation(unit_cxxflags)) ) default_esan_opts = '' config.substitutions.append(('%env_esan_opts=', -- cgit v1.2.3