-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_assign1_2.c
142 lines (105 loc) · 4.03 KB
/
test_assign1_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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "storage_mgr.h"
#include "dberror.h"
#include "test_helper.h"
// test name
char *testName;
/* test output files */
#define TESTPF "test_pagefile.bin"
/* prototypes for test functions */
static void testCreateOpenClose(void);
static void testSinglePageContent(void);
/* main function running all tests */
int
main (void)
{
testName = "";
initStorageManager();
testCreateOpenClose();
testSinglePageContent();
return 0;
}
/* check a return code. If it is not RC_OK then output a message, error description, and exit */
/* Try to create, open, and close a page file */
void
testCreateOpenClose(void)
{
SM_FileHandle fh;
testName = "test create open and close methods";
TEST_CHECK(createPageFile (TESTPF));
TEST_CHECK(openPageFile (TESTPF, &fh));
ASSERT_TRUE(strcmp(fh.fileName, TESTPF) == 0, "filename correct");
ASSERT_TRUE((fh.totalNumPages == 1), "expect 1 page in new file");
ASSERT_TRUE((fh.curPagePos == 0), "freshly opened file's page position should be 0");
TEST_CHECK(closePageFile (&fh));
TEST_CHECK(destroyPageFile (TESTPF));
// after destruction trying to open the file should cause an error
ASSERT_TRUE((openPageFile(TESTPF, &fh) != RC_OK), "opening non-existing file should return an error.");
TEST_DONE();
}
/* Try to create, open, and close a page file */
void
testSinglePageContent(void)
{
SM_FileHandle fh;
SM_PageHandle ph;
int i;
testName = "test single page content";
ph = (SM_PageHandle) malloc(PAGE_SIZE);
// create a new page file
TEST_CHECK(createPageFile (TESTPF));
TEST_CHECK(openPageFile (TESTPF, &fh));
printf("created and opened file\n");
// read first page into handle
TEST_CHECK(readFirstBlock (&fh, ph));
// the page should be empty (zero bytes)
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == 0), "expected zero byte in first page of freshly initialized page");
printf("first block was empty\n");
// change ph to be a string and write that one to disk
for (i=0; i < PAGE_SIZE; i++)
ph[i] = (i % 10) + '0';
TEST_CHECK(writeBlock(0, &fh, ph));
printf("writing first block\n");
// write string to current position of file.
TEST_CHECK(writeCurrentBlock(&fh, ph));
printf("writing Current block\n");
// write string to first page of file.
TEST_CHECK(writeBlock(1, &fh, ph));
printf("writing Intermediate Block 1 block\n");
// write string to current position of file.
TEST_CHECK(writeCurrentBlock(&fh, ph));
printf("writing Intermediate Block 1_current block\n");
// read back the page containing the string and check that it is correct
TEST_CHECK(readFirstBlock (&fh, ph));
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == (i % 10) + '0'), "character in page read from disk is the one we expected.");
printf("reading First block\n");
fh.curPagePos = 5000;//moving current page position to second block.
//Reading the previous block from file.
TEST_CHECK(readPreviousBlock (&fh, ph));
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == (i % 10) + '0'), "character in page read from disk is the one we expected.");
printf("reading Previous block\n");
//Reading the current block from file.
TEST_CHECK(readCurrentBlock (&fh, ph));
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == (i % 10) + '0'), "character in page read from disk is the one we expected.");
printf("reading Current block\n");
//Reading the specific block (2nd block in this case) from file.
TEST_CHECK(readBlock(2,&fh, ph));
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == (i % 10) + '0'), "character in page read from disk is the one we expected.");
//Reading the last block from file.
TEST_CHECK(readLastBlock (&fh, ph));
for (i=0; i < PAGE_SIZE; i++)
ASSERT_TRUE((ph[i] == (i % 10) + '0'), "character in page read from disk is the one we expected.");
//Testing ensureCapacity function.
TEST_CHECK(ensureCapacity(6,&fh));
// destroy new page file
TEST_CHECK(destroyPageFile (TESTPF));
TEST_DONE();
}