piece-chain

Research & implementation of a Piece Chain
git clone git://src.gearsix.net/piece-chain.git
Log | Files | Refs | Atom | README

test.c (4798B)


      1 
      2 #include "buf.h"
      3 
      4 #include <stdio.h>
      5 #include <signal.h>
      6 #include <string.h>
      7 #include <assert.h>
      8 
      9 static Buf *B;
     10 
     11 static FILE *in_f;
     12 static const char *in_p = "in.txt", *in_buf = "hello world";
     13 static FILE *out_f;
     14 static const char *out_p = "out.txt", *out_buf = "Hey buddy!";
     15 static FILE *tmp_f;
     16 static const char *tmp_p = "tmp.txt";
     17 
     18 
     19 static void setup();
     20 static void setdown();
     21 static void test_bufinit();
     22 static void test_bufidx();
     23 static void test_bufins();
     24 static void test_bufdel();
     25 static void test_bufins1();
     26 static void test_bufdel1();
     27 static void test_bufins2();
     28 static void test_bufout();
     29 
     30 
     31 int main()
     32 {
     33 	printf("test: ");
     34 	setup();	
     35 	signal(SIGABRT, setdown);
     36 	test_bufinit();
     37 	test_bufidx();
     38 	test_bufins();
     39 	test_bufdel();
     40 	test_bufins1();
     41 	test_bufdel1();
     42 	test_bufins2();
     43 	test_bufout();
     44 	setdown(0);
     45 	puts("success");
     46 }
     47 
     48 static void debugprint()
     49 {
     50 	Piece *p = B->tail;
     51 
     52 	printf("\nBuf: %p\n", (void *)B);
     53 	printf("size: %d, index: %d\n", (int)B->size, (int)B->idx);
     54 	do {
     55 		printf("%p", (void *)p);
     56 		if (p == B->tail) printf("<-tail");
     57 		if (p == B->head) printf("<-head");
     58 		if (p == B->pos) printf("<-pos");
     59 		printf("\n\toff: %d, len %d, f: %s\n", (int)p->off, (int)p->len,
     60 			(!p->f) ? "0" : (p->f == B->read) ? "read" : "append");
     61 	} while ((p = p->next));
     62 	printf("\n");
     63 }
     64 
     65 static void setup()
     66 {	
     67 	in_f = fopen(in_p, "w+b");
     68 	if (ferror(in_f)) perror("fopen in");
     69 	fputs(in_buf, in_f);
     70 
     71 	out_f = fopen(out_p, "w+b");
     72 	if (ferror(out_f)) perror("fopen out");
     73 
     74 	tmp_f = fopen(tmp_p, "w+b");
     75 	if (ferror(tmp_f)) perror("fopen tmp");
     76 }
     77 
     78 static void setdown(int n)
     79 {
     80 	buffree(B);
     81 	fclose(out_f);
     82 	fclose(in_f);
     83 	fclose(tmp_f);
     84 	if (n != SIGABRT) {
     85 		remove(in_p);
     86 		remove(out_p);
     87 		remove(tmp_p);
     88 	}
     89 }
     90 
     91 static void test_bufinit()
     92 {
     93 	Piece *p;
     94 
     95 	B = bufinit(in_f, tmp_f);
     96 
     97 	assert(B->read);
     98 	assert(B->append);
     99 	assert(B->pos == B->tail);
    100 	assert(B->pos == B->head);
    101 
    102 	assert((p = B->pos));
    103 	assert(p->f == B->read);
    104 	assert(p->off == 0);
    105 	assert(p->len == strlen(in_buf));
    106 	assert(p->prev == NULL);
    107 	assert(p->next == NULL);
    108 }
    109 
    110 static void test_bufidx()
    111 {
    112 	Piece *p; size_t idx = 5;
    113 
    114 	p = bufidx(B, idx);
    115 
    116 	assert(p == B->pos);
    117 	assert(p == B->head);
    118 	assert(p->prev == B->tail);
    119 	assert(p->f == B->read);
    120 	assert(p->off == idx);
    121 	assert(p->len == strlen(in_buf) - idx);
    122 }
    123 
    124 static void test_bufins()
    125 {
    126 	const char *buf = "y"; size_t idx = 2;
    127 	const size_t len = strlen(buf);
    128 
    129 	bufins(B, idx, buf);
    130 
    131 	assert(B->size == strlen(in_buf) + len);
    132 	assert(B->idx == idx + len);
    133 	assert(B->pos == B->tail->next->next);
    134 
    135 	assert(B->pos->f == B->read);
    136 	assert(B->pos->off == 2);
    137 	assert(B->pos->len == 3);
    138 	assert(B->pos->prev == B->tail->next);
    139 	assert(B->pos->next == B->head);
    140 }
    141 
    142 static void test_bufdel()
    143 {
    144 	size_t idx = 3; int len = B->size+1;
    145 	const size_t bsiz = B->size;
    146 
    147 	bufdel(B, idx, len);
    148 	
    149 	assert(B->size == bsiz-(bsiz-idx));
    150 	assert(B->idx == idx);
    151 	assert(B->pos == B->head);
    152 	assert(B->tail->next->next == B->pos);
    153 
    154 	assert(B->pos->f == B->read);
    155 	assert(B->pos->off == 11);
    156 	assert(B->pos->len == 0);
    157 	assert(B->pos->prev == B->tail->next);
    158 	assert(B->pos->next == NULL);
    159 }
    160 
    161 static void test_bufins1()
    162 {
    163 	size_t idx = 3; const char *buf = " buddy!";
    164 	const size_t len = strlen(buf), bsiz = B->size;
    165 
    166 	bufins(B, idx, buf);
    167 
    168 	assert(B->size == bsiz + len);
    169 	assert(B->idx == idx + len);
    170 	assert(B->pos == B->head);
    171 	assert(B->pos == B->tail->next->next->next);
    172 
    173 	assert(B->pos->prev->f == B->append);
    174 	assert(B->pos->prev->off == 1);
    175 	assert(B->pos->prev->len == len);
    176 	assert(B->pos->prev == B->tail->next->next);
    177 	assert(B->pos->next == NULL);
    178 }
    179 
    180 static void test_bufdel1()
    181 {
    182 	size_t idx = 0; int len = 1;
    183 	const size_t bsiz = B->size;
    184 
    185 	bufdel(B, idx, len);
    186 
    187 	assert(B->size == bsiz - len);
    188 	assert(B->idx == idx);
    189 	
    190 	assert(B->pos->prev == B->tail);
    191 	assert(B->pos->prev->f == 0);
    192 	assert(B->pos->prev->off == 0);
    193 	assert(B->pos->prev->len == 0);
    194 	assert(B->pos->prev->prev == 0);
    195 	assert(B->pos->prev->next == B->pos);
    196 
    197 	assert(B->pos->f == B->read);
    198 	assert(B->pos->off == 1);
    199 	assert(B->pos->len == (size_t)len);
    200 	assert(B->pos->prev == B->tail);
    201 	assert(B->pos->next->next->next == B->head);
    202 }
    203 
    204 static void test_bufins2()
    205 {
    206 	const char *buf = "H"; const size_t idx = 0;
    207 	size_t len = strlen(buf), bsiz = B->size;
    208 
    209 	bufins(B, idx, buf);
    210 
    211 	assert(B->size == bsiz + len);
    212 	assert(B->idx == idx + len);
    213 	assert(B->pos == B->tail->next->next);
    214 
    215 	assert(B->pos->prev == B->tail->next);
    216 	assert(B->pos->prev->f == B->append);
    217 	assert(B->pos->prev->off == 8);
    218 	assert(B->pos->prev->len == 1);
    219 	assert(B->pos->prev->prev == B->tail);
    220 	assert(B->pos->prev->next == B->pos);
    221 }
    222 
    223 static void test_bufout()
    224 {
    225 	size_t n = 0;
    226 	char buf[BUFSIZ] = {0};
    227 
    228 	bufout(B, out_f);
    229 	rewind(out_f);
    230 	n = fread(buf, 1, BUFSIZ, out_f);
    231 	assert(n > 0);
    232 	assert(strcmp(buf, out_buf) == 0);
    233 }