mirror of
https://github.com/fumiama/fumidb.git
synced 2026-06-13 06:50:26 +08:00
add types
This commit is contained in:
@@ -2,7 +2,9 @@ cmake_minimum_required(VERSION 3.0.0)
|
||||
project(fumidb_test VERSION 1.0)
|
||||
|
||||
add_executable(binary_test binary_test.c)
|
||||
add_executable(page_test page_test.c ../src/page.c ../src/file.c)
|
||||
add_executable(page_test page_test.c ../src/page.c ../src/file.c)
|
||||
add_executable(types_test types_test.c ../src/types.c ../src/types/int8.c ../src/page.c ../src/file.c)
|
||||
|
||||
add_test(test_binary binary_test COMMAND binary_test)
|
||||
add_test(test_page page_test COMMAND page_test)
|
||||
add_test(test_page page_test COMMAND page_test)
|
||||
add_test(test_types types_test COMMAND types_test)
|
||||
@@ -8,6 +8,14 @@
|
||||
void* pages[16];
|
||||
uint8_t nullpage[PAGESZ];
|
||||
|
||||
static uint64_t get_second_unused_block(int fd) {
|
||||
uint64_t ptr = get_first_unused_block(fd);
|
||||
if(!ptr) return 0;
|
||||
if(lseek(fd, ptr, SEEK_SET) < 0) return 0;
|
||||
readle64(fd, ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int fd = open("page_test_tmp.bin", O_RDWR | O_CREAT | O_TRUNC, 0644);
|
||||
if(fd < 0) {
|
||||
@@ -25,15 +33,20 @@ int main() {
|
||||
}
|
||||
puts("free!");
|
||||
free_page(fd, pages[15]);
|
||||
if(get_first_unused_block(fd) != 16*PAGESZ) return 5;
|
||||
if(get_second_unused_block(fd) != 16*PAGESZ) return 5;
|
||||
puts("free 15!");
|
||||
free_page(fd, pages[12]);
|
||||
if(get_first_unused_block(fd) != 13*PAGESZ) return 6;
|
||||
if(get_second_unused_block(fd) != 13*PAGESZ) return 6;
|
||||
puts("free 12!");
|
||||
free_page(fd, pages[1]);
|
||||
if(get_first_unused_block(fd) != 2*PAGESZ) return 7;
|
||||
if(get_second_unused_block(fd) != 2*PAGESZ) return 7;
|
||||
puts("free 1!");
|
||||
free_page(fd, pages[10]);
|
||||
if(get_first_unused_block(fd) != 2*PAGESZ) return 8;
|
||||
if(get_second_unused_block(fd) != 2*PAGESZ) return 8;
|
||||
puts("free 10!");
|
||||
free_page(fd, pages[9]);
|
||||
if(get_first_unused_block(fd) != 2*PAGESZ) return 8;
|
||||
if(get_second_unused_block(fd) != 2*PAGESZ) return 8;
|
||||
puts("free 9!");
|
||||
pages[1] = alloc_page(fd);
|
||||
if(le64(pages[1]-8) != (uint64_t)(2*PAGESZ)) {
|
||||
printf("1: %016llx != %016llx\n", le64(pages[1]-8), (uint64_t)(2*PAGESZ));
|
||||
@@ -72,30 +85,35 @@ int main() {
|
||||
uint8_t* blk4 = alloc_block(fd, 4095);
|
||||
memcpy(blk1, "hello world!", 13);
|
||||
sync_block(fd, blk1);
|
||||
lseek(fd, PAGESZ, SEEK_SET);
|
||||
lseek(fd, HEADERSZ, SEEK_SET);
|
||||
read(fd, blk2, 13);
|
||||
puts("hello world!");
|
||||
if(strcmp((const char *)blk2, (const char *)blk1)) {
|
||||
return 16;
|
||||
}
|
||||
puts("hello world 1!");
|
||||
sync_block(fd, blk2);
|
||||
lseek(fd, PAGESZ+40, SEEK_SET);
|
||||
lseek(fd, HEADERSZ+40, SEEK_SET);
|
||||
read(fd, blk3, 13);
|
||||
if(strcmp((const char *)blk3, (const char *)blk1)) {
|
||||
return 17;
|
||||
}
|
||||
puts("hello world 2!");
|
||||
sync_block(fd, blk3);
|
||||
lseek(fd, PAGESZ+40+22, SEEK_SET);
|
||||
lseek(fd, HEADERSZ+40+22, SEEK_SET);
|
||||
read(fd, blk4+222, 13);
|
||||
sync_block(fd, blk4);
|
||||
if(strcmp((const char *)&blk4[222], (const char *)blk1)) {
|
||||
return 18;
|
||||
}
|
||||
puts("hello world 3!");
|
||||
memset(blk1, 0, 40);
|
||||
lseek(fd, PAGESZ*2+222, SEEK_SET);
|
||||
lseek(fd, PAGESZ+222, SEEK_SET);
|
||||
read(fd, blk1, 13);
|
||||
if(strcmp((const char *)blk1, (const char *)blk2)) {
|
||||
return 19;
|
||||
}
|
||||
puts("hello world4!");
|
||||
if(free_block(fd, blk1)) {
|
||||
perror("free_block(fd, blk1)");
|
||||
return 20;
|
||||
@@ -115,7 +133,7 @@ int main() {
|
||||
blk1 = alloc_block(fd, 40+22+33);
|
||||
memcpy(blk1+44, "hello world!", 13);
|
||||
sync_block(fd, blk1);
|
||||
lseek(fd, PAGESZ+44, SEEK_SET);
|
||||
lseek(fd, HEADERSZ+44, SEEK_SET);
|
||||
char buf[13];
|
||||
read(fd, buf, 13);
|
||||
if(strcmp((const char *)&blk1[44], (const char *)buf)) {
|
||||
@@ -135,26 +153,26 @@ int main() {
|
||||
blk4 = alloc_block(fd, 4095);
|
||||
memcpy(blk1, "hello world!", 13);
|
||||
sync_block(fd, blk1);
|
||||
lseek(fd, PAGESZ, SEEK_SET);
|
||||
lseek(fd, HEADERSZ, SEEK_SET);
|
||||
read(fd, blk2, 13);
|
||||
if(strcmp((const char *)blk2, (const char *)blk1)) {
|
||||
return 16;
|
||||
}
|
||||
sync_block(fd, blk2);
|
||||
lseek(fd, PAGESZ+40, SEEK_SET);
|
||||
lseek(fd, HEADERSZ+40, SEEK_SET);
|
||||
read(fd, blk3, 13);
|
||||
if(strcmp((const char *)blk3, (const char *)blk1)) {
|
||||
return 17;
|
||||
}
|
||||
sync_block(fd, blk3);
|
||||
lseek(fd, PAGESZ+40+22, SEEK_SET);
|
||||
lseek(fd, HEADERSZ+40+22, SEEK_SET);
|
||||
read(fd, blk4+222, 13);
|
||||
sync_block(fd, blk4);
|
||||
if(strcmp((const char *)&blk4[222], (const char *)blk1)) {
|
||||
return 18;
|
||||
}
|
||||
memset(blk1, 0, 40);
|
||||
lseek(fd, PAGESZ*2+222, SEEK_SET);
|
||||
lseek(fd, PAGESZ+222, SEEK_SET);
|
||||
read(fd, blk1, 13);
|
||||
if(strcmp((const char *)blk1, (const char *)blk2)) {
|
||||
return 19;
|
||||
@@ -178,7 +196,7 @@ int main() {
|
||||
blk1 = alloc_block(fd, 40+22+33);
|
||||
memcpy(blk1+44, "hello world!", 13);
|
||||
sync_block(fd, blk1);
|
||||
lseek(fd, PAGESZ+44, SEEK_SET);
|
||||
lseek(fd, HEADERSZ+44, SEEK_SET);
|
||||
memset(buf, 0, 13);
|
||||
read(fd, buf, 13);
|
||||
if(strcmp((const char *)&blk1[44], (const char *)buf)) {
|
||||
|
||||
21
tests/types_test.c
Normal file
21
tests/types_test.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include "../include/binary.h"
|
||||
#include "../include/file.h"
|
||||
#include "../include/types.h"
|
||||
|
||||
int main() {
|
||||
int fd = open("types_test_tmp.bin", O_RDWR | O_CREAT | O_TRUNC, 0644);
|
||||
if(fd < 0) {
|
||||
perror("create");
|
||||
return 1;
|
||||
}
|
||||
if(init_file_header_page(fd) < 0) return 2;
|
||||
uint64_t ptr = create_index(fd, TYPE_INT8);
|
||||
if(!ptr) {
|
||||
perror("create_index");
|
||||
return 3;
|
||||
}
|
||||
close(fd);
|
||||
// remove("types_test_tmp.bin");
|
||||
}
|
||||
Reference in New Issue
Block a user