Home | History | Annotate | Download | only in unit
      1 //===-- tsan_clock_test.cc ------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file is a part of ThreadSanitizer (TSan), a race detector.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 #include "tsan_clock.h"
     14 #include "tsan_rtl.h"
     15 #include "gtest/gtest.h"
     16 
     17 namespace __tsan {
     18 
     19 TEST(Clock, VectorBasic) {
     20   ScopedInRtl in_rtl;
     21   ThreadClock clk;
     22   CHECK_EQ(clk.size(), 0);
     23   clk.tick(0);
     24   CHECK_EQ(clk.size(), 1);
     25   CHECK_EQ(clk.get(0), 1);
     26   clk.tick(3);
     27   CHECK_EQ(clk.size(), 4);
     28   CHECK_EQ(clk.get(0), 1);
     29   CHECK_EQ(clk.get(1), 0);
     30   CHECK_EQ(clk.get(2), 0);
     31   CHECK_EQ(clk.get(3), 1);
     32   clk.tick(3);
     33   CHECK_EQ(clk.get(3), 2);
     34 }
     35 
     36 TEST(Clock, ChunkedBasic) {
     37   ScopedInRtl in_rtl;
     38   ThreadClock vector;
     39   SyncClock chunked;
     40   CHECK_EQ(vector.size(), 0);
     41   CHECK_EQ(chunked.size(), 0);
     42   vector.acquire(&chunked);
     43   CHECK_EQ(vector.size(), 0);
     44   CHECK_EQ(chunked.size(), 0);
     45   vector.release(&chunked);
     46   CHECK_EQ(vector.size(), 0);
     47   CHECK_EQ(chunked.size(), 0);
     48   vector.acq_rel(&chunked);
     49   CHECK_EQ(vector.size(), 0);
     50   CHECK_EQ(chunked.size(), 0);
     51 }
     52 
     53 TEST(Clock, AcquireRelease) {
     54   ScopedInRtl in_rtl;
     55   ThreadClock vector1;
     56   vector1.tick(100);
     57   SyncClock chunked;
     58   vector1.release(&chunked);
     59   CHECK_EQ(chunked.size(), 101);
     60   ThreadClock vector2;
     61   vector2.acquire(&chunked);
     62   CHECK_EQ(vector2.size(), 101);
     63   CHECK_EQ(vector2.get(0), 0);
     64   CHECK_EQ(vector2.get(1), 0);
     65   CHECK_EQ(vector2.get(99), 0);
     66   CHECK_EQ(vector2.get(100), 1);
     67 }
     68 
     69 TEST(Clock, ManyThreads) {
     70   ScopedInRtl in_rtl;
     71   SyncClock chunked;
     72   for (int i = 0; i < 100; i++) {
     73     ThreadClock vector;
     74     vector.tick(i);
     75     vector.release(&chunked);
     76     CHECK_EQ(chunked.size(), i + 1);
     77     vector.acquire(&chunked);
     78     CHECK_EQ(vector.size(), i + 1);
     79   }
     80   ThreadClock vector;
     81   vector.acquire(&chunked);
     82   CHECK_EQ(vector.size(), 100);
     83   for (int i = 0; i < 100; i++)
     84     CHECK_EQ(vector.get(i), 1);
     85 }
     86 
     87 TEST(Clock, DifferentSizes) {
     88   ScopedInRtl in_rtl;
     89   {
     90     ThreadClock vector1;
     91     vector1.tick(10);
     92     ThreadClock vector2;
     93     vector2.tick(20);
     94     {
     95       SyncClock chunked;
     96       vector1.release(&chunked);
     97       CHECK_EQ(chunked.size(), 11);
     98       vector2.release(&chunked);
     99       CHECK_EQ(chunked.size(), 21);
    100     }
    101     {
    102       SyncClock chunked;
    103       vector2.release(&chunked);
    104       CHECK_EQ(chunked.size(), 21);
    105       vector1.release(&chunked);
    106       CHECK_EQ(chunked.size(), 21);
    107     }
    108     {
    109       SyncClock chunked;
    110       vector1.release(&chunked);
    111       vector2.acquire(&chunked);
    112       CHECK_EQ(vector2.size(), 21);
    113     }
    114     {
    115       SyncClock chunked;
    116       vector2.release(&chunked);
    117       vector1.acquire(&chunked);
    118       CHECK_EQ(vector1.size(), 21);
    119     }
    120   }
    121 }
    122 
    123 }  // namespace __tsan
    124