• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clangxx_unit -O0 %s -o %t 2>&1
2 // RUN: %env_esan_opts="record_snapshots=0" %run %t 2>&1 | FileCheck %s
3 
4 #include "esan/esan_circular_buffer.h"
5 #include "sanitizer_common/sanitizer_placement_new.h"
6 #include <assert.h>
7 #include <stdio.h>
8 
9 static const int TestBufCapacity = 4;
10 
11 // The buffer should have a capacity of TestBufCapacity.
testBuffer(__esan::CircularBuffer<int> * Buf)12 void testBuffer(__esan::CircularBuffer<int> *Buf) {
13   assert(Buf->size() == 0);
14   assert(Buf->empty());
15 
16   Buf->push_back(1);
17   assert(Buf->back() == 1);
18   assert((*Buf)[0] == 1);
19   assert(Buf->size() == 1);
20   assert(!Buf->empty());
21 
22   Buf->push_back(2);
23   Buf->push_back(3);
24   Buf->push_back(4);
25   Buf->push_back(5);
26   assert((*Buf)[0] == 2);
27   assert(Buf->size() == 4);
28 
29   Buf->pop_back();
30   assert((*Buf)[0] == 2);
31   assert(Buf->size() == 3);
32 
33   Buf->pop_back();
34   Buf->pop_back();
35   assert((*Buf)[0] == 2);
36   assert(Buf->size() == 1);
37   assert(!Buf->empty());
38 
39   Buf->pop_back();
40   assert(Buf->empty());
41 }
42 
main()43 int main()
44 {
45   // Test initialize/free.
46   __esan::CircularBuffer<int> GlobalBuf;
47   GlobalBuf.initialize(TestBufCapacity);
48   testBuffer(&GlobalBuf);
49   GlobalBuf.free();
50 
51   // Test constructor/free.
52   __esan::CircularBuffer<int> *LocalBuf;
53   static char placeholder[sizeof(*LocalBuf)];
54   LocalBuf = new(placeholder) __esan::CircularBuffer<int>(TestBufCapacity);
55   testBuffer(LocalBuf);
56   LocalBuf->free();
57 
58   fprintf(stderr, "All checks passed.\n");
59   // CHECK: All checks passed.
60   return 0;
61 }
62