blockFile

Save data in chunked in blocks in a file for creating databases
git clone https://noulin.net/git/blockFile.git
Log | Files | Refs | LICENSE

testBlockFileMem.c (4277B)


      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 #include <string.h>
      4 
      5 #define ck_assert_str_eq(a,b) a;b;
      6 #define ck_assert_str_ne(a,b) a;b;
      7 #define ck_assert_ptr_eq(a,b) a;b;
      8 #define ck_assert_ptr_ne(a,b) a;b;
      9 #define ck_assert_uint_eq(a,b) a;b;
     10 #define ck_assert_uint_ne(a,b) a;b;
     11 #define ck_assert_int_eq(a,b) a;b;
     12 #define ck_assert_int_ne(a,b) a;b;
     13 #define ck_assert(a) a;
     14 
     15 
     16 #include "libsheepyObject.h"
     17 #include "blockFile.h"
     18 
     19 int argc; char **argv;
     20 
     21 blockFilet bf;
     22 
     23 void basetT(void) {
     24 
     25   initiateBlockFile(&bf);
     26   freeO(&bf);
     27 
     28   blockFilet *rg = NULL;
     29   initiateAllocateBlockFile(&rg);
     30   terminateO(rg);
     31 
     32   rg = allocBlockFile("file.db");
     33 
     34   char *s = toStringO(rg);
     35 
     36   ck_assert_str_eq(s, "TODO - blockFile");
     37   free(s);
     38 
     39   blockFilet *rgDup = duplicateO(rg);
     40   terminateO(rgDup);
     41 
     42   terminateO(rg);
     43 
     44 }
     45 
     46 char *s1 = "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
     47 
     48 char *s2 = "222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222 cfp command printing current path in front of given parameters searchReplace command searches strings in files and replaces them with given searchReplace command searches strings in files and replaces them with";
     49 
     50 char *s3 = "get block emove data: remove(file, block): bool read block header find first block add blocks to freeF load: load(file, callback) read block 1, find first block add read blocks to a list find next chain of blocks > recreate index, or save the index delete block file: delete(file) close: close(file) delete file: deleteF(filename) add data to a block: addBlock(file, void*, length): u64 block add data in many blocks: addData(file, void*, length): u64[], end with 0";
     51 
     52 void addT(void) {
     53 
     54   initiateBlockFile(&bf);
     55 
     56   bf.f->open(&bf, "file.db");
     57   bf.f->delete(&bf);
     58   bf.f->open(&bf, "file.db");
     59 
     60   u64 r;
     61 
     62   r = addBFO(&bf, "qweqwe", 7);
     63   ck_assert_int_eq(r, 1);
     64 
     65   // add compressed block
     66   r = addBFO(&bf, s2, strlen(s2)+1);
     67   ck_assert_int_eq(r, 2);
     68 
     69   // add big block
     70   r = addBFO(&bf, s1, strlen(s1)+1);
     71   ck_assert_int_eq(r, 5);
     72 
     73   freeO(&bf);
     74 
     75   // no free blocks
     76   ck_assert_int_eq(fileSize("file.dbx"), 0);
     77 
     78 }
     79 
     80 void getT(void) {
     81 
     82   initiateBlockFile(&bf);
     83 
     84   bf.f->open(&bf, "file.db");
     85 
     86   bufBlockFilet res;
     87 
     88   res = getBFO(&bf, 5);
     89   ck_assert_str_eq((char*)res.data, s1);
     90   ck_assert_int_eq(res.len, strlen(s1)+1);
     91   free(res.data);
     92 
     93   res = getBFO(&bf, 2);
     94   ck_assert_str_eq((char*)res.data, s2);
     95   ck_assert_int_eq(res.len, strlen(s2)+1);
     96   free(res.data);
     97 
     98   res = getBFO(&bf, 1);
     99   ck_assert_str_eq((char*)res.data, "qweqwe");
    100   ck_assert_int_eq(res.len, 7);
    101   free(res.data);
    102 
    103   freeO(&bf);
    104 
    105   // no free blocks
    106   ck_assert_int_eq(fileSize("file.dbx"), 0);
    107 
    108 }
    109 
    110 void removeT(void) {
    111 
    112   initiateBlockFile(&bf);
    113 
    114   bf.f->open(&bf, "file.db");
    115 
    116   bufBlockFilet res;
    117   bool r;
    118   u64 R;
    119 
    120   // remove block 1
    121   r = removeBFO(&bf, 1);
    122   ck_assert(r);
    123 
    124   // remove block 1 again, fails
    125   r = removeBFO(&bf, 1);
    126   ck_assert(!r);
    127 
    128   // get a deleted block
    129   res = getBFO(&bf, 1);
    130   ck_assert_ptr_eq(res.data, NULL);
    131   ck_assert_int_eq(res.len, 0);
    132 
    133   // add block
    134   R = addBFO(&bf, "asdasd", 7);
    135   ck_assert_int_eq(R, 1);
    136 
    137   // remove block 5
    138   r = removeBFO(&bf, 5);
    139   ck_assert(r);
    140 
    141   // add block
    142   R = addBFO(&bf, "zxczxc", 7);
    143   ck_assert_int_eq(R, 7);
    144 
    145   // add another big block
    146   R = addBFO(&bf, s3, strlen(s3)+1);
    147   ck_assert_int_eq(R, 6);
    148 
    149   // remove block 2
    150   r = removeBFO(&bf, 2);
    151   ck_assert(r);
    152 
    153   // remove block 2
    154   r = removeBFO(&bf, 2);
    155   ck_assert(!r);
    156 
    157   freeO(&bf);
    158 
    159   // 3 free blocks
    160   ck_assert_int_eq(fileSize("file.dbx"), 12);
    161 
    162 }
    163 
    164 bool loadFunc(void UNUSED *c, u64 UNUSED b, bufBlockFilet data) {
    165   /* cast(char*, s, data.data); */
    166   /* logVarG(b); */
    167   /* logVarG(s); */
    168   /* logVarG(data.len); */
    169   free(data.data);
    170   return true;
    171 }
    172 
    173 void loadT(void) {
    174 
    175   initiateBlockFile(&bf);
    176 
    177   bf.f->open(&bf, "file.db");
    178 
    179   loadBFO(&bf, NULL, loadFunc);
    180   /* bufBlockFilet res; */
    181   /* bool r; */
    182   /* u64 R; */
    183 
    184   freeO(&bf);
    185 
    186   // 3 free blocks
    187   ck_assert_int_eq(fileSize("file.dbx"), 12);
    188 
    189 }
    190 
    191 
    192 int main(int n, char**v) {
    193 
    194 initLibsheepy(v[0]);
    195 setLogMode(LOG_VERBOSE);
    196 
    197 basetT();
    198 addT();
    199 getT();
    200 removeT();
    201 loadT();
    202 }