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

testBlockFile.c (4817B)


      1 #! /usr/bin/env sheepy
      2 /* or direct path to sheepy: #! /usr/local/bin/sheepy */
      3 
      4 /** \file
      5  * Each test must independent and self contained
      6  */
      7 
      8 #include <check.h>
      9 
     10 //START MEM TEST ANCHOR
     11 
     12 #include "libsheepyObject.h"
     13 #include "blockFile.h"
     14 
     15 int argc; char **argv;
     16 
     17 blockFilet bf;
     18 
     19 START_TEST(basetT)
     20 
     21   initiateBlockFile(&bf);
     22   freeO(&bf);
     23 
     24   blockFilet *rg = NULL;
     25   initiateAllocateBlockFile(&rg);
     26   terminateO(rg);
     27 
     28   rg = allocBlockFile("file.db");
     29 
     30   char *s = toStringO(rg);
     31 
     32   ck_assert_str_eq(s, "TODO - blockFile");
     33   free(s);
     34 
     35   blockFilet *rgDup = duplicateO(rg);
     36   terminateO(rgDup);
     37 
     38   terminateO(rg);
     39 
     40 END_TEST
     41 
     42 char *s1 = "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
     43 
     44 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";
     45 
     46 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";
     47 
     48 START_TEST(addT)
     49 
     50   initiateBlockFile(&bf);
     51 
     52   bf.f->open(&bf, "file.db");
     53   bf.f->delete(&bf);
     54   bf.f->open(&bf, "file.db");
     55 
     56   u64 r;
     57 
     58   r = addBFO(&bf, "qweqwe", 7);
     59   ck_assert_int_eq(r, 1);
     60 
     61   // add compressed block
     62   r = addBFO(&bf, s2, strlen(s2)+1);
     63   ck_assert_int_eq(r, 2);
     64 
     65   // add big block
     66   r = addBFO(&bf, s1, strlen(s1)+1);
     67   ck_assert_int_eq(r, 5);
     68 
     69   freeO(&bf);
     70 
     71   // no free blocks
     72   ck_assert_int_eq(fileSize("file.dbx"), 0);
     73 
     74 END_TEST
     75 
     76 START_TEST(getT)
     77 
     78   initiateBlockFile(&bf);
     79 
     80   bf.f->open(&bf, "file.db");
     81 
     82   bufBlockFilet res;
     83 
     84   res = getBFO(&bf, 5);
     85   ck_assert_str_eq((char*)res.data, s1);
     86   ck_assert_int_eq(res.len, strlen(s1)+1);
     87   free(res.data);
     88 
     89   res = getBFO(&bf, 2);
     90   ck_assert_str_eq((char*)res.data, s2);
     91   ck_assert_int_eq(res.len, strlen(s2)+1);
     92   free(res.data);
     93 
     94   res = getBFO(&bf, 1);
     95   ck_assert_str_eq((char*)res.data, "qweqwe");
     96   ck_assert_int_eq(res.len, 7);
     97   free(res.data);
     98 
     99   freeO(&bf);
    100 
    101   // no free blocks
    102   ck_assert_int_eq(fileSize("file.dbx"), 0);
    103 
    104 END_TEST
    105 
    106 START_TEST(removeT)
    107 
    108   initiateBlockFile(&bf);
    109 
    110   bf.f->open(&bf, "file.db");
    111 
    112   bufBlockFilet res;
    113   bool r;
    114   u64 R;
    115 
    116   // remove block 1
    117   r = removeBFO(&bf, 1);
    118   ck_assert(r);
    119 
    120   // remove block 1 again, fails
    121   r = removeBFO(&bf, 1);
    122   ck_assert(!r);
    123 
    124   // get a deleted block
    125   res = getBFO(&bf, 1);
    126   ck_assert_ptr_eq(res.data, NULL);
    127   ck_assert_int_eq(res.len, 0);
    128 
    129   // add block
    130   R = addBFO(&bf, "asdasd", 7);
    131   ck_assert_int_eq(R, 1);
    132 
    133   // remove block 5
    134   r = removeBFO(&bf, 5);
    135   ck_assert(r);
    136 
    137   // add block
    138   R = addBFO(&bf, "zxczxc", 7);
    139   ck_assert_int_eq(R, 7);
    140 
    141   // add another big block
    142   R = addBFO(&bf, s3, strlen(s3)+1);
    143   ck_assert_int_eq(R, 6);
    144 
    145   // remove block 2
    146   r = removeBFO(&bf, 2);
    147   ck_assert(r);
    148 
    149   // remove block 2
    150   r = removeBFO(&bf, 2);
    151   ck_assert(!r);
    152 
    153   freeO(&bf);
    154 
    155   // 3 free blocks
    156   ck_assert_int_eq(fileSize("file.dbx"), 12);
    157 
    158 END_TEST
    159 
    160 bool loadFunc(void UNUSED *c, u64 UNUSED b, bufBlockFilet data) {
    161   /* cast(char*, s, data.data); */
    162   /* logVarG(b); */
    163   /* logVarG(s); */
    164   /* logVarG(data.len); */
    165   free(data.data);
    166   return true;
    167 }
    168 
    169 START_TEST(loadT)
    170 
    171   initiateBlockFile(&bf);
    172 
    173   bf.f->open(&bf, "file.db");
    174 
    175   loadBFO(&bf, NULL, loadFunc);
    176   /* bufBlockFilet res; */
    177   /* bool r; */
    178   /* u64 R; */
    179 
    180   freeO(&bf);
    181 
    182   // 3 free blocks
    183   ck_assert_int_eq(fileSize("file.dbx"), 12);
    184 
    185 END_TEST
    186 
    187 Suite * blockFileSuite(void) {
    188   Suite *s;
    189   TCase *tc_core;
    190 
    191   s = suite_create("blockFile");
    192 
    193   /* Core test case */
    194   tc_core = tcase_create("Core");
    195 
    196 
    197   tcase_add_test(tc_core, basetT);
    198   tcase_add_test(tc_core, addT);
    199   tcase_add_test(tc_core, getT);
    200   tcase_add_test(tc_core, removeT);
    201   tcase_add_test(tc_core, loadT);
    202 
    203   suite_add_tcase(s, tc_core);
    204 
    205   return s;
    206 }
    207 
    208 int main(int ARGC, char** ARGV) {
    209 
    210   argc = ARGC; argv = ARGV;
    211 
    212   //dont initialize libsheepy, it conflicts with libcheck - initLibsheepy(ARGV[0]);
    213   setLogMode(LOG_VERBOSE);
    214 
    215   int number_failed;
    216   Suite *s;
    217   SRunner *sr;
    218 
    219   s = blockFileSuite();
    220   sr = srunner_create(s);
    221 
    222   srunner_run_all(sr, CK_NORMAL);
    223   number_failed = srunner_ntests_failed(sr);
    224   srunner_free(sr);
    225 
    226   exit((number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
    227 }