Ejemplo n.º 1
0
TEST(AtomicVector, append) {
  AtomicVector<int> vec;
  Refcount<int> rc;
  const int M = 100;
  for (int i = 0; i < M; i++) {
    vec.append(i * 3 + 1);
  }

  for (int i = 0; i < M; i++) {
    auto val = vec.read(i);
    assert(val == i * 3 + 1);
    rc.dec(val);
  }
}
Ejemplo n.º 2
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);
  }
}