Browse code

Add move test. Fix op_move code.

Dario Rodriguez authored on 18/03/2019 21:56:38
Showing 2 changed files
... ...
@@ -305,14 +305,12 @@ redata_chunk_movedata(redata_t *redata, int chunkfrom, long posfrom, int chunkto
305 305
                 /* from==to */
306 306
                 rechunk_t *chunk=redata->chunks[chunkfrom];
307 307
                 memcpy(redata->tmpchunk->data,chunk->data+posfrom,size);
308
-                if(posto>posfrom) {
309
-                        int inside=(posto-posfrom)-size;
310
-                        memmove(chunk->data+posfrom,chunk->data+posto-inside,inside);
311
-                        memcpy(chunk->data+posfrom+inside,redata->tmpchunk->data,size);
312
-                } else {
313
-                        int inside=(posto-posfrom);
314
-                        memmove(chunk->data+posfrom+size-inside,chunk->data+posto,inside);
308
+                if(posfrom>posto) {
309
+                        memmove(chunk->data+posto+size,chunk->data+posto,posfrom-posto);
315 310
                         memcpy(chunk->data+posto,redata->tmpchunk->data,size);
311
+                } else {
312
+                        memmove(chunk->data+posfrom,chunk->data+posfrom+size,posto-posfrom-size);
313
+                        memcpy(chunk->data+posto-size,redata->tmpchunk->data,size);
316 314
                 }
317 315
                 chunk->whatin_fresh=0;
318 316
         }
... ...
@@ -1168,8 +1166,8 @@ redata_op_move(redata_t *redata, long posorig, long size, long posdest, undostac
1168 1166
                                 return(-1); /* invalid pos or insuf. mem */
1169 1167
                         if(pos!=0)
1170 1168
                                 chunkno++;
1171
-                        if(spos==0)
1172
-                                schunkno--;
1169
+                        if(spos!=0)
1170
+                                schunkno++;
1173 1171
                         if(dpos!=0)
1174 1172
                                 dchunkno++;
1175 1173
                         if(chunkno<0 || schunkno<0 || dchunkno<0
... ...
@@ -1178,12 +1176,12 @@ redata_op_move(redata_t *redata, long posorig, long size, long posdest, undostac
1178 1176
                         /* reorder inplace inverting the bytes (as in flipping a image) */
1179 1177
                         if(chunkno<dchunkno) {
1180 1178
                                 meminvert(redata->chunks+chunkno,redata->chunks+dchunkno);
1181
-                                meminvert(redata->chunks+chunkno,redata->chunks+dchunkno-(schunkno-chunkno));
1182 1179
                                 meminvert(redata->chunks+dchunkno-(schunkno-chunkno),redata->chunks+dchunkno);
1180
+                                meminvert(redata->chunks+chunkno,redata->chunks+dchunkno-(schunkno-chunkno));
1183 1181
                         } else {
1184
-                                meminvert(redata->chunks+dchunkno,redata->chunks+chunkno);
1182
+                                meminvert(redata->chunks+dchunkno,redata->chunks+schunkno);
1185 1183
                                 meminvert(redata->chunks+dchunkno,redata->chunks+dchunkno+(schunkno-chunkno));
1186
-                                meminvert(redata->chunks+dchunkno+(schunkno-chunkno),redata->chunks+chunkno);
1184
+                                meminvert(redata->chunks+dchunkno+(schunkno-chunkno),redata->chunks+schunkno);
1187 1185
                         }
1188 1186
                 }
1189 1187
         }
... ...
@@ -43,26 +43,38 @@ main(int argc, char *argv[])
43 43
         static int sizes[]={-1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,31,32,33,63,64,65,127,128,129,1023,1024,1025,16384};
44 44
         test_t tests[]={
45 45
                 {"newfile_16",test_newfile,PREFIX "FILE", NULL, 16, 0},
46
-#if 0 /* more newfile tests (VERY slow) */
47 46
                 {"newfile_1024",test_newfile,PREFIX "FILE", NULL, 1024, 0},
48 47
                 {"newfile_32767",test_newfile,PREFIX "FILE", NULL, 32767, 0},
49 48
                 {"newfile_32768",test_newfile,PREFIX "FILE", NULL, 32768, 0},
50 49
                 {"newfile_32769",test_newfile,PREFIX "FILE", NULL, 32769, 0},
51 50
                 {"newfile_131072",test_newfile,PREFIX "FILE", NULL, 131072, 0},
52
-#endif
53 51
                 {"testedit_add",test_edit,PREFIX "EDIT", "A0$Testing add$",0,0},
54 52
                 {"testedit_del",test_edit,PREFIX "EDIT", "A0$Testing add/del$D8$add/$",0,0},
53
+                {"testedit_move",test_edit,PREFIX "EDIT", "A0$This is a text to move.$M17,7$ move$",0,0},
55 54
         };
55
+        int flag_exit=0,flag_all=0;
56 56
         redata_t *redata;
57 57
         int i,s;
58 58
         int nerrors,total;
59 59
         char *res;
60
+        for(i=1;i<argc;i++) {
61
+                if(strcmp(argv[i],"--help")==0) {
62
+                        fprintf(stderr,"Syntax: %s [--all] [--exit] [--help]\nExplanation:\n\t--all: do even the slow tests\n\t--exit: exit program at first unsuccessful test\n\t--help: this text\n",argv[0]);
63
+                        return(1);
64
+                } else if(strcmp(argv[i],"--all")==0) {
65
+                        flag_all=1;
66
+                } else if(strcmp(argv[i],"--exit")==0) {
67
+                        flag_exit=1;
68
+                }
69
+        }
60 70
         nerrors=0;
61 71
         total=0;
62 72
         for(s=0;s<(sizeof(sizes)/sizeof(sizes[0]));s++) {
63 73
                 if(sizes[s]!=-1)
64 74
                         fprintf(stderr,"SIZE: %i\n",sizes[s]);
65 75
                 for(i=0;i<(sizeof(tests)/sizeof(tests[0]));i++,total++) {
76
+                        if(!flag_all && tests[i].int1>=1024)
77
+                                continue; /* too slow: skip unless testing --all */
66 78
                         if((redata=redata_init())==NULL) {
67 79
                                 fprintf(stderr,"ERROR: problem initializing redata module\n");
68 80
                                 return(1);
... ...
@@ -76,11 +88,11 @@ main(int argc, char *argv[])
76 88
                         } else {
77 89
                                 fprintf(stderr," ERROR: %s\n",res);
78 90
                                 nerrors++;
79
-#if 0
80
-/* exit on first error */
81
-s=sizeof(sizes)/sizeof(sizes[0]);
82
-break;
83
-#endif
91
+                                if(flag_exit) {
92
+                                        /* exit on first error */
93
+                                        s=sizeof(sizes)/sizeof(sizes[0]);
94
+                                        break;
95
+                                }
84 96
                         }
85 97
                         redata_free(redata),redata=NULL;
86 98
                 }
... ...
@@ -168,7 +180,7 @@ test_edit(redata_t *redata, char *filename, char *edits, int filesize, int seed)
168 180
         int k;
169 181
         int cursize,maxsize;
170 182
         char *ptr;
171
-        int l;
183
+        int l,o;
172 184
         char *mem;
173 185
         char endcode;
174 186
         char *ptrend;
... ...
@@ -248,6 +260,53 @@ if(k!=0) redata_save(redata,"test.pre");
248 260
                                 cursize-=size;
249 261
                                 ptr+=size;
250 262
                                 ptr+=(*ptr!='\0')?1:0;
263
+                        } else if(*ptr=='M') {
264
+                                /* M<origpos>,<destpos><endchar><text><endchar> */
265
+                                ptr++;
266
+                                errno=0;
267
+                                l=(int)strtol(ptr,&ptr,10);
268
+                                if(errno!=0) {
269
+                                        if(mem!=NULL)
270
+                                                free(mem),mem=NULL;
271
+                                        return("test_edit(): error parsing position");
272
+                                }
273
+                                ptr+=(*ptr==',')?1:0;
274
+                                errno=0;
275
+                                o=(int)strtol(ptr,&ptr,10);
276
+                                if(errno!=0) {
277
+                                        if(mem!=NULL)
278
+                                                free(mem),mem=NULL;
279
+                                        return("test_edit(): error parsing position");
280
+                                }
281
+                                endcode=*ptr;
282
+                                ptr+=(*ptr!='\0')?1:0;
283
+                                ptrend=strchr(ptr,endcode);
284
+                                ptrend=(ptrend==NULL)?ptr+strlen(ptr):ptrend;
285
+                                size=ptrend-ptr;
286
+                                if(l<0 || (l+size)>cursize || o<0 || o>cursize || (o>=l && o<(l+size))) {
287
+                                        if(mem!=NULL)
288
+                                                free(mem),mem=NULL;
289
+                                        return("test_edit(): internal error: invalid pasition or size");
290
+                                }
291
+                                if(k!=0) {
292
+                                        if(memcmp(mem+l,ptr,size)!=0
293
+                                          || redata_data_compare(redata,l,ptr,size)!=0) {
294
+                                                if(mem!=NULL)
295
+                                                        free(mem),mem=NULL;
296
+                                                return("test_edit(): internal error: move data doesn't match");
297
+                                        }
298
+                                        /* editing */
299
+                                        if(l>o) {
300
+                                                memmove(mem+o+size,mem+o,l-o);
301
+                                                memcpy(mem+o,ptr,size);
302
+                                        } else {
303
+                                                memmove(mem+l,mem+l+size,o-l-size);
304
+                                                memcpy(mem+o-size,ptr,size);
305
+                                        }
306
+                                        redata_op_move(redata,l,size,o,NULL);
307
+                                }
308
+                                ptr+=size;
309
+                                ptr+=(*ptr!='\0')?1:0;
251 310
                         } else {
252 311
                                 if(mem!=NULL)
253 312
                                         free(mem),mem=NULL;