-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_assign1_1.c
103 lines (75 loc) · 2.53 KB
/
test_assign1_1.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
#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");
// 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");
// destroy new page file
TEST_CHECK(destroyPageFile (TESTPF));
TEST_DONE();
}