예제 #1
0
TEST(AtomicVector, write) {
  AtomicVector<int> vec;
  Refcount<int> rc;
  // Some single-threaded write tests.
  bool sawExc = false;
  try {
    vec.write(1, 666); // Past end of vector
  } catch(runtime_error& re) {
    sawExc = true;
  }
  ASSERT_EQ(sawExc, true);

  // Append a few.
  const int N = 17;
  const int M = 1000;
  for (int i = 1; i < M; i++) {
    ASSERT_EQ(rc.get(i, true), 0);
  }
  for (int i = 0; i < N; i++) {
    vec.append(3 * i + 1);
  }

  for (int i = 0; i < N; i++) {
    ASSERT_EQ(rc.get(3 * i + 1), 1);
  }

  for (int i = 0; i < M; i++) {
    auto idx = i % N;
    vec.write(idx, i + 1);
  }

  for (int i = 0; i < N; i++) {
    auto val = vec.read(i);
    rc.dec(val);
    ASSERT_EQ(rc.get(val), 1);
  }
}
예제 #2
0
TEST(AtomicVector, mpRefcount) {
  Refcount<int> rc;
  // Test refcount reasoning.
  for (int i = 0; i < 12; i++) {
    assert(rc.get(1, true) == 0);
    AtomicVector<int> lval;
    auto numThreads = mptest([&](int idx) {
        lval.append(1);
    });
    ASSERT_EQ(lval.size(), numThreads);
    assert(rc.get(1) == numThreads);

    (void) mptest([&](int idx) {
        lval.write(idx, idx + 1);
    });
    for (int i = 0; i < numThreads; i++) {
      ASSERT_EQ(rc.get(i + 1), 1);
    }
  }
  rc.assertClear();
}