5 * Created by Victor Grishchenko on 3/22/09.
6 * Copyright 2009-2012 TECHNISCHE UNIVERSITEIT DELFT. All rights reserved.
10 #include <gtest/gtest.h>
14 using namespace swift;
22 uint8_t rand_norm (uint8_t lim) {
23 long rnd = RANDOM() & ((1<<lim)-1);
32 TEST(FreemapTest,Freemap) {
34 const bin_t top(30,0);
36 typedef std::pair<int,bin_t> timebin_t;
37 typedef std::set<timebin_t> ts_t;
39 for (int t=0; t<1000000; t++) {
44 if (t<500000 || t>504000) {
45 uint8_t lr = rand_norm(28);
46 bin_t alloc = space.find_empty();
47 while (alloc.layer()>lr)
49 ASSERT_NE(0ULL,~alloc.toUInt());
50 EXPECT_TRUE(space.is_empty(alloc));
52 long dealloc_time = 1<<rand_norm(22);
54 printf("alloc 2**%i starting at %lli for %li ticks\n",
55 (int)lr,alloc.toUInt(),dealloc_time);
58 to_free.insert(timebin_t(dealloc_time,alloc));
60 // now, the red-black tree
61 while (to_free.begin()->first<=t) {
62 bin_t freebin = to_free.begin()->second;
63 to_free.erase(to_free.begin());
66 printf("freed at %lli\n",
70 // log: space taken, gaps, binmap cells, tree cells
71 int cells = space.cells_number();
74 printf("time %i cells used %i blocks %i\n",
75 t,cells,(int)to_free.size());
77 //space.dump("space");
79 for(ts_t::iterator i=to_free.begin(); i!=to_free.end(); i++)
80 space.reset(i->second);
81 EXPECT_TRUE(space.is_empty(top));
84 int main (int argc, char** argv) {
85 testing::InitGoogleTest(&argc, argv);
86 return RUN_ALL_TESTS();