-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_assign2_2.c
148 lines (119 loc) · 3.38 KB
/
test_assign2_2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "storage_mgr.h"
#include "buffer_mgr_stat.h"
#include "buffer_mgr.h"
#include "dberror.h"
#include "test_helper.h"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <semaphore.h>
// var to store the current test's name
char *testName;
// check whether two the content of a buffer pool is the same as an expected content
// (given in the format produced by sprintPoolContent)
#define ASSERT_EQUALS_POOL(expected,bm,message) \
do { \
char *real; \
char *_exp = (char *) (expected); \
real = sprintPoolContent(bm); \
if (strcmp((_exp),real) != 0) \
{ \
printf("[%s-%s-L%i-%s] FAILED: expected <%s> but was <%s>: %s\n",TEST_INFO, _exp, real, message); \
free(real); \
exit(1); \
} \
printf("[%s-%s-L%i-%s] OK: expected <%s> and was <%s>: %s\n",TEST_INFO, _exp, real, message); \
free(real); \
} while(0)
// test and helper methods
static void testCreatingAndReadingDummyPages (void);
static void createDummyPages(BM_BufferPool *bm, int num);
static void checkDummyPages(BM_BufferPool *bm, int num);
static void testReadPage (void);
static void testsimpleFIFO (void);
static void testLRU (void);
// main method
int
main (void)
{
int res;
pthread_t newthread;
testName = "";
res=pthread_create(&newthread,NULL,testsimpleFIFO,NULL);
if(res!=0)
{
printf("Thread creation failed");
exit(1);
}
res=pthread_join(newthread,NULL);
testsimpleFIFO();
return 0;
}
void
createDummyPages(BM_BufferPool *bm, int num)
{
int i;
BM_PageHandle *h = MAKE_PAGE_HANDLE();
CHECK(initBufferPool(bm, "testbuffer.bin", 3, RS_FIFO, NULL));
for (i = 0; i < num; i++)
{
CHECK(pinPage(bm, h, i));
sprintf(h->data, "%s-%i", "Page", h->pageNum);
CHECK(markDirty(bm, h));
CHECK(unpinPage(bm,h));
}
CHECK(shutdownBufferPool(bm));
free(h);
}
void
checkDummyPages(BM_BufferPool *bm, int num)
{
int i;
BM_PageHandle *h = MAKE_PAGE_HANDLE();
char *expected = malloc(sizeof(char) * 512);
CHECK(initBufferPool(bm, "testbuffer.bin", 3, RS_FIFO, NULL));
for (i = 0; i < num; i++)
{
CHECK(pinPage(bm, h, i));
sprintf(expected, "%s-%i", "Page", h->pageNum);
ASSERT_EQUALS_STRING(expected, h->data, "reading back dummy page content");
CHECK(unpinPage(bm,h));
}
CHECK(shutdownBufferPool(bm));
free(expected);
free(h);
}
void
testsimpleFIFO ()
{
printf("FIFO Execution in new thread : \n " );
// expected results
const char *poolContents[] = {
"[0 0],[-1 0],[-1 0]" ,
"[0 0],[1 0],[-1 0]",
"[0 0],[1 0],[2 0]",
"[3 0],[1 0],[2 0]"
};
const int requests[] = {0,1,2,3,4,4,5,6,0};
const int numLinRequests = 4;
int i;
BM_BufferPool *bm = MAKE_POOL();
BM_PageHandle *h = MAKE_PAGE_HANDLE();
testName = "Testing FIFO page replacement";
CHECK(createPageFile("testbuffer.bin"));
createDummyPages(bm, 100);
CHECK(initBufferPool(bm, "testbuffer.bin", 3, RS_FIFO, NULL));
// reading some pages linearly with direct unpin and no modifications
for(i = 0; i < numLinRequests; i++)
{
pinPage(bm, h, requests[i]);
unpinPage(bm, h);
ASSERT_EQUALS_POOL(poolContents[i], bm, "check pool content");
}
CHECK(shutdownBufferPool(bm));
CHECK(destroyPageFile("testbuffer.bin"));
free(bm);
free(h);
TEST_DONE();
}