Example #1
0
void tst_mpq() {
    tst_prev_power_2();
    set_str_bug();
    bug2();
    bug1();
    tst0();
    tst1();
    tst2();
}
Example #2
0
void bug1() {
  struct test_s a1  = { .id = 1 };
  struct test_s a2  = { .id = 2 };
  struct test_s a3  = { .id = 3 };

  LRU_TYPE(test_s) cache;

  LRU_INIT(&cache, 4);
  
  LRU_INSERT(test_s, &cache, &a1);
  LRU_INSERT(test_s, &cache, &a2);
  LRU_INSERT(test_s, &cache, &a3);
  LRU_FIND(test_s, &cache, &a2);
  LRU_FIND(test_s, &cache, &a1);
  
  LRU_REMOVE(test_s, &cache, LRU_HEAD(&cache));
  LRU_REMOVE(test_s, &cache, LRU_HEAD(&cache));
  LRU_REMOVE(test_s, &cache, LRU_HEAD(&cache));
  assert(LRU_HEAD(&cache) == 0);
}


int main() {
  struct test_s a1  = { .id = 1 };
  struct test_s a2  = { .id = 2 };
  struct test_s a3  = { .id = 3 };
  struct test_s a4  = { .id = 4 };
  struct test_s a5  = { .id = 5 };
  struct test_s a12 = { .id = 1 };

  LRU_TYPE(test_s) cache;

  LRU_INIT(&cache, 4);

  assert(LRU_FIND(test_s, &cache, &a1) == 0);

  LRU_INSERT(test_s, &cache, &a1);
  assert(LRU_FIND(test_s, &cache, &a1) == &a1);

  LRU_INSERT(test_s, &cache, &a2);
  LRU_INSERT(test_s, &cache, &a3);
  LRU_INSERT(test_s, &cache, &a4);
  LRU_INSERT(test_s, &cache, &a5);
  assert(free_calls == 1);
  assert(LRU_FIND(test_s, &cache, &a1) == 0  );
  assert(LRU_FIND(test_s, &cache, &a2) == &a2);
  assert(LRU_FIND(test_s, &cache, &a3) == &a3);
  assert(LRU_FIND(test_s, &cache, &a4) == &a4);
  assert(LRU_FIND(test_s, &cache, &a5) == &a5);
  
  LRU_REMOVE(test_s, &cache, &a5);
  assert(free_calls == 2);
  assert(LRU_FIND(test_s, &cache, &a1) == 0  );
  assert(LRU_FIND(test_s, &cache, &a2) == &a2);
  assert(LRU_FIND(test_s, &cache, &a3) == &a3);
  assert(LRU_FIND(test_s, &cache, &a4) == &a4);
  assert(LRU_FIND(test_s, &cache, &a5) == 0  );
  
  LRU_INSERT(test_s, &cache, &a1);
  assert(free_calls == 2);
  assert(LRU_FIND(test_s, &cache, &a1 ) == &a1 );
  assert(LRU_FIND(test_s, &cache, &a12) != &a12);
  assert(LRU_FIND(test_s, &cache, &a2 ) == &a2 );
  assert(LRU_FIND(test_s, &cache, &a3 ) == &a3 );
  assert(LRU_FIND(test_s, &cache, &a4 ) == &a4 );
  assert(LRU_FIND(test_s, &cache, &a5 ) == 0   );
  LRU_INSERT(test_s, &cache, &a12);
  assert(free_calls == 3);
  assert(LRU_FIND(test_s, &cache, &a1 ) != &a1 );
  assert(LRU_FIND(test_s, &cache, &a12) == &a12);
  assert(LRU_FIND(test_s, &cache, &a2 ) == &a2 );
  assert(LRU_FIND(test_s, &cache, &a3 ) == &a3 );
  assert(LRU_FIND(test_s, &cache, &a4 ) == &a4 );
  assert(LRU_FIND(test_s, &cache, &a5 ) == 0   );

  struct test_s* p;
  while ((p = LRU_HEAD(&cache))) {
    LRU_REMOVE(test_s, &cache, p);
  }
  assert(free_calls == 7);  /* 1, 2, 3 and 4 were still in there so +4. */

  assert(LRU_FIND(test_s, &cache, &a1) == 0);
  assert(LRU_FIND(test_s, &cache, &a2) == 0);
  assert(LRU_FIND(test_s, &cache, &a3) == 0);
  assert(LRU_FIND(test_s, &cache, &a4) == 0);
  assert(LRU_FIND(test_s, &cache, &a5) == 0);


  bug1();


  printf("All tests passed.\n");

  return 0;
}