mirror of
https://github.com/fumiama/fumidb.git
synced 2026-06-05 16:50:33 +08:00
174 lines
5.7 KiB
C
174 lines
5.7 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include "../include/binary.h"
|
|
#include "../include/file.h"
|
|
#include "../include/page.h"
|
|
#include "../include/types.h"
|
|
#include "../include/types/int8.h"
|
|
|
|
uint8_t buf[10290];
|
|
|
|
int main() {
|
|
/* test int8 */
|
|
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)) return 2;
|
|
void* index = create_index(fd, TYPE_INT8, buf);
|
|
if(!index) {
|
|
perror("create_int8_index");
|
|
return 3;
|
|
}
|
|
if(le64(index-10) != HEADERSZ) {
|
|
printf("%016llx\n", le64(index-10));
|
|
return 4;
|
|
}
|
|
if(le16(index-2) != INT8_INDEX_SZ) {
|
|
printf("%04x\n", le16(index-2));
|
|
return 5;
|
|
}
|
|
insert_item(fd, TYPE_INT8, index, 1, 3456432);
|
|
insert_item(fd, TYPE_INT8, index, 3, 7654323456);
|
|
insert_item(fd, TYPE_INT8, index, 45, 345743415);
|
|
insert_item(fd, TYPE_INT8, index, 67, 56787145);
|
|
insert_item(fd, TYPE_INT8, index, 123, 123567854424);
|
|
if(count_items(fd, TYPE_INT8, index) != 5) return 6;
|
|
if(find_item_by_key(fd, TYPE_INT8, index, 1) != 3456432) return 6;
|
|
if(find_item_by_key(fd, TYPE_INT8, index, 3) != 7654323456) return 7;
|
|
if(find_item_by_key(fd, TYPE_INT8, index, 45) != 345743415) return 8;
|
|
if(find_item_by_key(fd, TYPE_INT8, index, 67) != 56787145) return 9;
|
|
if(find_item_by_key(fd, TYPE_INT8, index, 123) != 123567854424) return 10;
|
|
if(find_item_by_key(fd, TYPE_INT8, index, 255) != 0) return 11;
|
|
index = NULL;
|
|
close(fd);
|
|
fd = open("types_test_tmp.bin", O_RDWR, 0644);
|
|
memset(buf, 0, INT8_INDEX_SZ+10);
|
|
index = load_index(fd, TYPE_INT8, HEADERSZ, buf);
|
|
if(find_item_by_key(fd, TYPE_INT8, index, 1) != 3456432) return 6;
|
|
if(find_item_by_key(fd, TYPE_INT8, index, 3) != 7654323456) return 7;
|
|
if(find_item_by_key(fd, TYPE_INT8, index, 45) != 345743415) return 8;
|
|
if(find_item_by_key(fd, TYPE_INT8, index, 67) != 56787145) return 9;
|
|
if(find_item_by_key(fd, TYPE_INT8, index, 123) != 123567854424) return 10;
|
|
if(find_item_by_key(fd, TYPE_INT8, index, 255) != 0) return 11;
|
|
remove_item_by_key(fd, TYPE_INT8, index, 123);
|
|
if(find_item_by_key(fd, TYPE_INT8, index, 123) != 0) return 12;
|
|
if(count_items(fd, TYPE_INT8, index) != 4) return 13;
|
|
if(remove_index(fd, TYPE_INT8, index)) return 14;
|
|
index = create_index(fd, TYPE_INT8, buf);
|
|
if(!index) {
|
|
perror("create_int8_index");
|
|
return 3;
|
|
}
|
|
if(count_items(fd, TYPE_INT8, index) != 0) return 15;
|
|
close(fd);
|
|
/* end test int8 */
|
|
|
|
/* test int16 */
|
|
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;
|
|
index = create_index(fd, TYPE_INT16, buf);
|
|
if(!index) {
|
|
perror("create_int16_index");
|
|
return 3;
|
|
}
|
|
int cnt = 0;
|
|
for(int i = 57344; i < 65536+257; i++, cnt++) {
|
|
int n;
|
|
if((n=count_items(fd, TYPE_INT16, index)) != cnt) {
|
|
printf("%d != %d\n", cnt, n);
|
|
return 4;
|
|
}
|
|
if(insert_item(fd, TYPE_INT16, index, (key_t)i, i)) {
|
|
printf("%u ", (uint16_t)i);
|
|
fflush(stdout);
|
|
perror("insert_int16_item");
|
|
return 4;
|
|
}
|
|
}
|
|
|
|
for(int i = 57344; i < 65536+257; i++) {
|
|
if((int)find_item_by_key(fd, TYPE_INT16, index, (key_t)i) != i) {
|
|
printf("%u ", (uint16_t)i);
|
|
fflush(stdout);
|
|
perror("find_item_by_key");
|
|
return 5;
|
|
}
|
|
}
|
|
|
|
close(fd);
|
|
fd = open("types_test_tmp.bin", O_RDWR, 0644);
|
|
memset(buf, 0, sizeof(buf));
|
|
index = load_index(fd, TYPE_INT16, HEADERSZ, buf);
|
|
|
|
for(int i = 65500; i < 65536+10; i++, cnt--) {
|
|
int n;
|
|
if((n=count_items(fd, TYPE_INT16, index)) != cnt) {
|
|
printf("%d != %d\n", cnt, n);
|
|
return 6;
|
|
}
|
|
if(remove_item_by_key(fd, TYPE_INT16, index, (key_t)i) != i) {
|
|
printf("%u ", (uint16_t)i);
|
|
fflush(stdout);
|
|
perror("remove_item_by_key");
|
|
return 6;
|
|
}
|
|
}
|
|
|
|
for(int i = 65500; i < 65536+10; i++) {
|
|
if((int)find_item_by_key(fd, TYPE_INT16, index, (key_t)i) != 0) {
|
|
printf("%u ", (uint16_t)i);
|
|
fflush(stdout);
|
|
perror("find_item_by_key");
|
|
return 7;
|
|
}
|
|
}
|
|
|
|
for(int i = 57344; i < 65500; i++) {
|
|
if((int)find_item_by_key(fd, TYPE_INT16, index, (key_t)i) != i) {
|
|
printf("%u ", (uint16_t)i);
|
|
fflush(stdout);
|
|
perror("find_item_by_key");
|
|
return 8;
|
|
}
|
|
}
|
|
|
|
for(int i = 65536+10; i < 65536+257; i++) {
|
|
if((int)find_item_by_key(fd, TYPE_INT16, index, (key_t)i) != i) {
|
|
printf("%u ", (uint16_t)i);
|
|
fflush(stdout);
|
|
perror("find_item_by_key");
|
|
return 9;
|
|
}
|
|
}
|
|
|
|
for(int i = 57000; i < 65536+259; i++) {
|
|
if(insert_item(fd, TYPE_INT16, index, (key_t)i, i*1234)) {
|
|
printf("%u ", (uint16_t)i);
|
|
fflush(stdout);
|
|
perror("insert_int16_item");
|
|
return 10;
|
|
}
|
|
}
|
|
|
|
for(int i = 57000; i < 65536+259; i++) {
|
|
if((int)find_item_by_key(fd, TYPE_INT16, index, (key_t)i) != i*1234) {
|
|
printf("%u ", (uint16_t)i);
|
|
fflush(stdout);
|
|
perror("find_item_by_key");
|
|
return 11;
|
|
}
|
|
}
|
|
|
|
close(fd);
|
|
/* end test int16 */
|
|
// remove("types_test_tmp.bin");
|
|
}
|