* [PATCH v2 0/4] prepare for histogram diff @ 2011-07-06 16:38 Tay Ray Chuan 2011-07-06 16:38 ` [PATCH v2 1/4] xdiff/xprepare: use memset() Tay Ray Chuan 2011-07-07 4:23 ` [PATCH v3 0/4] prepare for histogram diff Tay Ray Chuan 0 siblings, 2 replies; 16+ messages in thread From: Tay Ray Chuan @ 2011-07-06 16:38 UTC (permalink / raw) To: Git Mailing List; +Cc: Junio C Hamano, Shawn O. Pearce Tay Ray Chuan (4): xdiff/xprepare: use memset() xdiff/xprepare: refactor abort cleanups xdiff/xpatience: factor out fall-back-diff function t4033-diff-patience: factor out tests t/lib-diff-patience.sh | 163 ++++++++++++++++++++++++++++++++++++++++++++++ t/t4033-diff-patience.sh | 162 +-------------------------------------------- xdiff/xpatience.c | 27 +------- xdiff/xprepare.c | 104 +++++++++++------------------- xdiff/xutils.c | 31 +++++++++ xdiff/xutils.h | 2 + 6 files changed, 238 insertions(+), 251 deletions(-) create mode 100644 t/lib-diff-patience.sh -- 1.7.3.4.721.g4666.dirty ^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v2 1/4] xdiff/xprepare: use memset() 2011-07-06 16:38 [PATCH v2 0/4] prepare for histogram diff Tay Ray Chuan @ 2011-07-06 16:38 ` Tay Ray Chuan 2011-07-06 16:38 ` [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups Tay Ray Chuan 2011-07-07 4:23 ` [PATCH v3 0/4] prepare for histogram diff Tay Ray Chuan 1 sibling, 1 reply; 16+ messages in thread From: Tay Ray Chuan @ 2011-07-06 16:38 UTC (permalink / raw) To: Git Mailing List; +Cc: Junio C Hamano, Shawn O. Pearce Use memset() instead of a for loop to initialize. This could give a performance advantage. Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> --- xdiff/xprepare.c | 10 +++------- 1 files changed, 3 insertions(+), 7 deletions(-) diff --git a/xdiff/xprepare.c b/xdiff/xprepare.c index 1689085..783631a 100644 --- a/xdiff/xprepare.c +++ b/xdiff/xprepare.c @@ -64,8 +64,6 @@ static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2); static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) { - long i; - cf->flags = flags; cf->hbits = xdl_hashbits((unsigned int) size); @@ -80,8 +78,7 @@ static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) { xdl_cha_free(&cf->ncha); return -1; } - for (i = 0; i < cf->hsize; i++) - cf->rchash[i] = NULL; + memset(cf->rchash, 0, cf->hsize * sizeof(xdlclass_t *)); cf->count = 0; @@ -136,7 +133,7 @@ static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp, xdlclassifier_t *cf, xdfile_t *xdf) { unsigned int hbits; - long i, nrec, hsize, bsize; + long nrec, hsize, bsize; unsigned long hav; char const *blk, *cur, *top, *prev; xrecord_t *crec; @@ -164,8 +161,7 @@ static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp, xdl_cha_free(&xdf->rcha); return -1; } - for (i = 0; i < hsize; i++) - rhash[i] = NULL; + memset(rhash, 0, hsize * sizeof(xrecord_t *)); nrec = 0; if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) { -- 1.7.3.4.721.g4666.dirty ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups 2011-07-06 16:38 ` [PATCH v2 1/4] xdiff/xprepare: use memset() Tay Ray Chuan @ 2011-07-06 16:38 ` Tay Ray Chuan 2011-07-06 16:38 ` [PATCH v2 3/4] xdiff/xpatience: factor out fall-back-diff function Tay Ray Chuan 2011-07-06 17:57 ` [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups Junio C Hamano 0 siblings, 2 replies; 16+ messages in thread From: Tay Ray Chuan @ 2011-07-06 16:38 UTC (permalink / raw) To: Git Mailing List; +Cc: Junio C Hamano, Shawn O. Pearce Group free()'s that are called when a malloc() fails in xdl_prepare_ctx(), making for more readable code. Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> --- xdiff/xprepare.c | 94 +++++++++++++++++++---------------------------------- 1 files changed, 34 insertions(+), 60 deletions(-) diff --git a/xdiff/xprepare.c b/xdiff/xprepare.c index 783631a..6168327 100644 --- a/xdiff/xprepare.c +++ b/xdiff/xprepare.c @@ -143,24 +143,15 @@ static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp, char *rchg; long *rindex; - if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) { - - return -1; - } - if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) { - - xdl_cha_free(&xdf->rcha); - return -1; - } + if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) + goto abort_rcha; + if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) + goto abort_recs; hbits = xdl_hashbits((unsigned int) narec); hsize = 1 << hbits; - if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) { - - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; - } + if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) + goto abort_rhash; memset(rhash, 0, hsize * sizeof(xrecord_t *)); nrec = 0; @@ -175,63 +166,30 @@ static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp, hav = xdl_hash_record(&cur, top, xpp->flags); if (nrec >= narec) { narec *= 2; - if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) { - - xdl_free(rhash); - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; - } + if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) + goto abort_rrecs; recs = rrecs; } - if (!(crec = xdl_cha_alloc(&xdf->rcha))) { - - xdl_free(rhash); - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; - } + if (!(crec = xdl_cha_alloc(&xdf->rcha))) + goto abort_crec; crec->ptr = prev; crec->size = (long) (cur - prev); crec->ha = hav; recs[nrec++] = crec; - if (xdl_classify_record(cf, rhash, hbits, crec) < 0) { - - xdl_free(rhash); - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; - } + if (xdl_classify_record(cf, rhash, hbits, crec) < 0) + goto abort_classify; } } - if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) { - - xdl_free(rhash); - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; - } + if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) + goto abort_rchg; memset(rchg, 0, (nrec + 2) * sizeof(char)); - if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) { - - xdl_free(rchg); - xdl_free(rhash); - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; - } - if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) { - - xdl_free(rindex); - xdl_free(rchg); - xdl_free(rhash); - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; - } + if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) + goto abort_rindex; + if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) + goto abort_ha; xdf->nrec = nrec; xdf->recs = recs; @@ -245,6 +203,22 @@ static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp, xdf->dend = nrec - 1; return 0; + +abort_ha: + xdl_free(rindex); +abort_rindex: + xdl_free(rchg); +abort_rchg: +abort_classify: +abort_crec: +abort_rrecs: + xdl_free(rhash); +abort_rhash: + xdl_free(recs); +abort_recs: + xdl_cha_free(&xdf->rcha); +abort_rcha: + return -1; } -- 1.7.3.4.721.g4666.dirty ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v2 3/4] xdiff/xpatience: factor out fall-back-diff function 2011-07-06 16:38 ` [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups Tay Ray Chuan @ 2011-07-06 16:38 ` Tay Ray Chuan 2011-07-06 16:38 ` [PATCH v2 4/4] t4033-diff-patience: factor out tests Tay Ray Chuan 2011-07-06 17:57 ` [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups Junio C Hamano 1 sibling, 1 reply; 16+ messages in thread From: Tay Ray Chuan @ 2011-07-06 16:38 UTC (permalink / raw) To: Git Mailing List; +Cc: Junio C Hamano, Shawn O. Pearce This is in preparation for the histogram diff algorithm, which will also re-use much of the code to call the default Meyers diff algorithm. Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> --- xdiff/xpatience.c | 27 ++------------------------- xdiff/xutils.c | 31 +++++++++++++++++++++++++++++++ xdiff/xutils.h | 2 ++ 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/xdiff/xpatience.c b/xdiff/xpatience.c index e42c16a..fdd7d02 100644 --- a/xdiff/xpatience.c +++ b/xdiff/xpatience.c @@ -287,34 +287,11 @@ static int walk_common_sequence(struct hashmap *map, struct entry *first, static int fall_back_to_classic_diff(struct hashmap *map, int line1, int count1, int line2, int count2) { - /* - * This probably does not work outside Git, since - * we have a very simple mmfile structure. - * - * Note: ideally, we would reuse the prepared environment, but - * the libxdiff interface does not (yet) allow for diffing only - * ranges of lines instead of the whole files. - */ - mmfile_t subfile1, subfile2; xpparam_t xpp; - xdfenv_t env; - - subfile1.ptr = (char *)map->env->xdf1.recs[line1 - 1]->ptr; - subfile1.size = map->env->xdf1.recs[line1 + count1 - 2]->ptr + - map->env->xdf1.recs[line1 + count1 - 2]->size - subfile1.ptr; - subfile2.ptr = (char *)map->env->xdf2.recs[line2 - 1]->ptr; - subfile2.size = map->env->xdf2.recs[line2 + count2 - 2]->ptr + - map->env->xdf2.recs[line2 + count2 - 2]->size - subfile2.ptr; xpp.flags = map->xpp->flags & ~XDF_PATIENCE_DIFF; - if (xdl_do_diff(&subfile1, &subfile2, &xpp, &env) < 0) - return -1; - memcpy(map->env->xdf1.rchg + line1 - 1, env.xdf1.rchg, count1); - memcpy(map->env->xdf2.rchg + line2 - 1, env.xdf2.rchg, count2); - - xdl_free_env(&env); - - return 0; + return xdl_fall_back_diff(map->env, &xpp, + line1, count1, line2, count2); } /* diff --git a/xdiff/xutils.c b/xdiff/xutils.c index ab65034..ded7c32 100644 --- a/xdiff/xutils.c +++ b/xdiff/xutils.c @@ -402,3 +402,34 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, return 0; } + +int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp, + int line1, int count1, int line2, int count2) +{ + /* + * This probably does not work outside Git, since + * we have a very simple mmfile structure. + * + * Note: ideally, we would reuse the prepared environment, but + * the libxdiff interface does not (yet) allow for diffing only + * ranges of lines instead of the whole files. + */ + mmfile_t subfile1, subfile2; + xdfenv_t env; + + subfile1.ptr = (char *)diff_env->xdf1.recs[line1 - 1]->ptr; + subfile1.size = diff_env->xdf1.recs[line1 + count1 - 2]->ptr + + diff_env->xdf1.recs[line1 + count1 - 2]->size - subfile1.ptr; + subfile2.ptr = (char *)diff_env->xdf2.recs[line2 - 1]->ptr; + subfile2.size = diff_env->xdf2.recs[line2 + count2 - 2]->ptr + + diff_env->xdf2.recs[line2 + count2 - 2]->size - subfile2.ptr; + if (xdl_do_diff(&subfile1, &subfile2, xpp, &env) < 0) + return -1; + + memcpy(diff_env->xdf1.rchg + line1 - 1, env.xdf1.rchg, count1); + memcpy(diff_env->xdf2.rchg + line2 - 1, env.xdf2.rchg, count2); + + xdl_free_env(&env); + + return 0; +} diff --git a/xdiff/xutils.h b/xdiff/xutils.h index d5de829..674a657 100644 --- a/xdiff/xutils.h +++ b/xdiff/xutils.h @@ -41,6 +41,8 @@ int xdl_num_out(char *out, long val); long xdl_atol(char const *str, char const **next); int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, const char *func, long funclen, xdemitcb_t *ecb); +int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp, + int line1, int count1, int line2, int count2); -- 1.7.3.4.721.g4666.dirty ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v2 4/4] t4033-diff-patience: factor out tests 2011-07-06 16:38 ` [PATCH v2 3/4] xdiff/xpatience: factor out fall-back-diff function Tay Ray Chuan @ 2011-07-06 16:38 ` Tay Ray Chuan 2011-07-06 18:04 ` Junio C Hamano 0 siblings, 1 reply; 16+ messages in thread From: Tay Ray Chuan @ 2011-07-06 16:38 UTC (permalink / raw) To: Git Mailing List; +Cc: Junio C Hamano, Shawn O. Pearce Group the test cases into two functions, test_diff_(frobnitz|unique). This in preparation for the histogram diff algorithm, which would also re-use these test cases. Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> --- Ram: the rename is detected in git-log, but not in the diffstat. The expected and actual content, which makes up the bulk of content, is unchanged, but the 'surrounding' changes should be pretty easy to spot. t/lib-diff-patience.sh | 163 ++++++++++++++++++++++++++++++++++++++++++++++ t/t4033-diff-patience.sh | 162 +-------------------------------------------- 2 files changed, 166 insertions(+), 159 deletions(-) create mode 100644 t/lib-diff-patience.sh diff --git a/t/lib-diff-patience.sh b/t/lib-diff-patience.sh new file mode 100644 index 0000000..761bb09 --- /dev/null +++ b/t/lib-diff-patience.sh @@ -0,0 +1,163 @@ +test_diff_frobnitz() { + cat >file1 <<\EOF +#include <stdio.h> + +// Frobs foo heartily +int frobnitz(int foo) +{ + int i; + for(i = 0; i < 10; i++) + { + printf("Your answer is: "); + printf("%d\n", foo); + } +} + +int fact(int n) +{ + if(n > 1) + { + return fact(n-1) * n; + } + return 1; +} + +int main(int argc, char **argv) +{ + frobnitz(fact(10)); +} +EOF + + cat >file2 <<\EOF +#include <stdio.h> + +int fib(int n) +{ + if(n > 2) + { + return fib(n-1) + fib(n-2); + } + return 1; +} + +// Frobs foo heartily +int frobnitz(int foo) +{ + int i; + for(i = 0; i < 10; i++) + { + printf("%d\n", foo); + } +} + +int main(int argc, char **argv) +{ + frobnitz(fib(10)); +} +EOF + + cat >expect <<\EOF +diff --git a/file1 b/file2 +index 6faa5a3..e3af329 100644 +--- a/file1 ++++ b/file2 +@@ -1,26 +1,25 @@ + #include <stdio.h> + ++int fib(int n) ++{ ++ if(n > 2) ++ { ++ return fib(n-1) + fib(n-2); ++ } ++ return 1; ++} ++ + // Frobs foo heartily + int frobnitz(int foo) + { + int i; + for(i = 0; i < 10; i++) + { +- printf("Your answer is: "); + printf("%d\n", foo); + } + } + +-int fact(int n) +-{ +- if(n > 1) +- { +- return fact(n-1) * n; +- } +- return 1; +-} +- + int main(int argc, char **argv) + { +- frobnitz(fact(10)); ++ frobnitz(fib(10)); + } +EOF + + STRATEGY=$1 + + test_expect_success "$STRATEGY diff" ' + test_must_fail git diff --no-index "--$STRATEGY" file1 file2 > output && + test_cmp expect output + ' + + test_expect_success "$STRATEGY diff output is valid" ' + mv file2 expect && + git apply < output && + test_cmp expect file2 + ' +} + +test_diff_unique() { + cat >uniq1 <<\EOF +1 +2 +3 +4 +5 +6 +EOF + + cat >uniq2 <<\EOF +a +b +c +d +e +f +EOF + + cat >expect <<\EOF +diff --git a/uniq1 b/uniq2 +index b414108..0fdf397 100644 +--- a/uniq1 ++++ b/uniq2 +@@ -1,6 +1,6 @@ +-1 +-2 +-3 +-4 +-5 +-6 ++a ++b ++c ++d ++e ++f +EOF + + STRATEGY=$1 + + test_expect_success 'completely different files' ' + test_must_fail git diff --no-index "--$STRATEGY" uniq1 uniq2 > output && + test_cmp expect output + ' +} + diff --git a/t/t4033-diff-patience.sh b/t/t4033-diff-patience.sh index 1eb1498..9fe9921 100755 --- a/t/t4033-diff-patience.sh +++ b/t/t4033-diff-patience.sh @@ -3,166 +3,10 @@ test_description='patience diff algorithm' . ./test-lib.sh +. "$TEST_DIRECTORY"/lib-diff-patience.sh -cat >file1 <<\EOF -#include <stdio.h> +test_diff_frobnitz "patience" -// Frobs foo heartily -int frobnitz(int foo) -{ - int i; - for(i = 0; i < 10; i++) - { - printf("Your answer is: "); - printf("%d\n", foo); - } -} - -int fact(int n) -{ - if(n > 1) - { - return fact(n-1) * n; - } - return 1; -} - -int main(int argc, char **argv) -{ - frobnitz(fact(10)); -} -EOF - -cat >file2 <<\EOF -#include <stdio.h> - -int fib(int n) -{ - if(n > 2) - { - return fib(n-1) + fib(n-2); - } - return 1; -} - -// Frobs foo heartily -int frobnitz(int foo) -{ - int i; - for(i = 0; i < 10; i++) - { - printf("%d\n", foo); - } -} - -int main(int argc, char **argv) -{ - frobnitz(fib(10)); -} -EOF - -cat >expect <<\EOF -diff --git a/file1 b/file2 -index 6faa5a3..e3af329 100644 ---- a/file1 -+++ b/file2 -@@ -1,26 +1,25 @@ - #include <stdio.h> - -+int fib(int n) -+{ -+ if(n > 2) -+ { -+ return fib(n-1) + fib(n-2); -+ } -+ return 1; -+} -+ - // Frobs foo heartily - int frobnitz(int foo) - { - int i; - for(i = 0; i < 10; i++) - { -- printf("Your answer is: "); - printf("%d\n", foo); - } - } - --int fact(int n) --{ -- if(n > 1) -- { -- return fact(n-1) * n; -- } -- return 1; --} -- - int main(int argc, char **argv) - { -- frobnitz(fact(10)); -+ frobnitz(fib(10)); - } -EOF - -test_expect_success 'patience diff' ' - - test_must_fail git diff --no-index --patience file1 file2 > output && - test_cmp expect output - -' - -test_expect_success 'patience diff output is valid' ' - - mv file2 expect && - git apply < output && - test_cmp expect file2 - -' - -cat >uniq1 <<\EOF -1 -2 -3 -4 -5 -6 -EOF - -cat >uniq2 <<\EOF -a -b -c -d -e -f -EOF - -cat >expect <<\EOF -diff --git a/uniq1 b/uniq2 -index b414108..0fdf397 100644 ---- a/uniq1 -+++ b/uniq2 -@@ -1,6 +1,6 @@ --1 --2 --3 --4 --5 --6 -+a -+b -+c -+d -+e -+f -EOF - -test_expect_success 'completely different files' ' - - test_must_fail git diff --no-index --patience uniq1 uniq2 > output && - test_cmp expect output - -' +test_diff_unique "patience" test_done -- 1.7.3.4.721.g4666.dirty ^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH v2 4/4] t4033-diff-patience: factor out tests 2011-07-06 16:38 ` [PATCH v2 4/4] t4033-diff-patience: factor out tests Tay Ray Chuan @ 2011-07-06 18:04 ` Junio C Hamano 0 siblings, 0 replies; 16+ messages in thread From: Junio C Hamano @ 2011-07-06 18:04 UTC (permalink / raw) To: Tay Ray Chuan; +Cc: Git Mailing List, Junio C Hamano, Shawn O. Pearce Tay Ray Chuan <rctay89@gmail.com> writes: > Group the test cases into two functions, test_diff_(frobnitz|unique). > This in preparation for the histogram diff algorithm, which would also > re-use these test cases. > > Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> > --- > > Ram: the rename is detected in git-log, but not in the diffstat. The > expected and actual content, which makes up the bulk of content, is > unchanged, but the 'surrounding' changes should be pretty easy to spot. > > t/lib-diff-patience.sh | 163 ++++++++++++++++++++++++++++++++++++++++++++++ Perhaps name this "t/lib-diff-alternative.sh" (or "strategy"), as this will soon be for testing non-default diff backend algorithms, not just patience diff? > t/t4033-diff-patience.sh | 162 +-------------------------------------------- > 2 files changed, 166 insertions(+), 159 deletions(-) > create mode 100644 t/lib-diff-patience.sh > > diff --git a/t/lib-diff-patience.sh b/t/lib-diff-patience.sh > new file mode 100644 > index 0000000..761bb09 > --- /dev/null > +++ b/t/lib-diff-patience.sh > @@ -0,0 +1,163 @@ > ... > + STRATEGY=$1 > + > + test_expect_success "$STRATEGY diff" ' > + test_must_fail git diff --no-index "--$STRATEGY" file1 file2 > output && > + test_cmp expect output > + ' > + > + test_expect_success "$STRATEGY diff output is valid" ' > + mv file2 expect && > + git apply < output && > + test_cmp expect file2 > + ' > +} ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups 2011-07-06 16:38 ` [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups Tay Ray Chuan 2011-07-06 16:38 ` [PATCH v2 3/4] xdiff/xpatience: factor out fall-back-diff function Tay Ray Chuan @ 2011-07-06 17:57 ` Junio C Hamano 2011-07-06 18:10 ` Tay Ray Chuan 1 sibling, 1 reply; 16+ messages in thread From: Junio C Hamano @ 2011-07-06 17:57 UTC (permalink / raw) To: Tay Ray Chuan; +Cc: Git Mailing List, Junio C Hamano, Shawn O. Pearce Tay Ray Chuan <rctay89@gmail.com> writes: > Group free()'s that are called when a malloc() fails in > xdl_prepare_ctx(), making for more readable code. Nicer, but I wonder if we can get away with a single label that lets us abort with freeing whatever we have allocated by making sure that the various pointer fields and variables are initialized to NULL, which may lead to even more readable code. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups 2011-07-06 17:57 ` [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups Junio C Hamano @ 2011-07-06 18:10 ` Tay Ray Chuan 2011-07-06 23:31 ` Junio C Hamano 0 siblings, 1 reply; 16+ messages in thread From: Tay Ray Chuan @ 2011-07-06 18:10 UTC (permalink / raw) To: Junio C Hamano; +Cc: Git Mailing List, Shawn O. Pearce On Thu, Jul 7, 2011 at 1:57 AM, Junio C Hamano <gitster@pobox.com> wrote: > Tay Ray Chuan <rctay89@gmail.com> writes: > >> Group free()'s that are called when a malloc() fails in >> xdl_prepare_ctx(), making for more readable code. > > Nicer, but I wonder if we can get away with a single label that lets us > abort with freeing whatever we have allocated by making sure that the > various pointer fields and variables are initialized to NULL, which may > lead to even more readable code. Pardon my dullness. Do you mean to check if the various fields are set to NULL upon allocation, and free()'ing them if so? -- Cheers, Ray Chuan ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups 2011-07-06 18:10 ` Tay Ray Chuan @ 2011-07-06 23:31 ` Junio C Hamano 2011-07-07 3:55 ` Phil Hord 0 siblings, 1 reply; 16+ messages in thread From: Junio C Hamano @ 2011-07-06 23:31 UTC (permalink / raw) To: Tay Ray Chuan; +Cc: Git Mailing List, Shawn O. Pearce Tay Ray Chuan <rctay89@gmail.com> writes: > On Thu, Jul 7, 2011 at 1:57 AM, Junio C Hamano <gitster@pobox.com> wrote: >> Tay Ray Chuan <rctay89@gmail.com> writes: >> >>> Group free()'s that are called when a malloc() fails in >>> xdl_prepare_ctx(), making for more readable code. >> >> Nicer, but I wonder if we can get away with a single label that lets us >> abort with freeing whatever we have allocated by making sure that the >> various pointer fields and variables are initialized to NULL, which may >> lead to even more readable code. > > Pardon my dullness. Do you mean to check if the various fields are set > to NULL upon allocation, and free()'ing them if so? What I meant was that, instead of doing this: func() { if (somefunc(&A, ...) < 0) goto failA; ... do something ... B = someotheralloc(); if (!B) goto failB; ... do something ... C = yetanotheralloc(); if (!C) goto failC; ... do things using A, B, C ... return 0; failC: free(B); failB: free(A); failA: return -1; } it would be easier to follow if you did: func() { A = B = C = NULL; if (somefunc(&A, ...) < 0) goto fail; ... do something ... B = someotheralloc(); if (!B) goto fail; ... do something ... C = yetanotheralloc(); if (!C) goto fail; ... do things using A, B, C ... fail: free(B); free(A); return -1; } Especially when you have more than 2 such fail labels. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups 2011-07-06 23:31 ` Junio C Hamano @ 2011-07-07 3:55 ` Phil Hord 2011-07-07 4:05 ` Tay Ray Chuan 0 siblings, 1 reply; 16+ messages in thread From: Phil Hord @ 2011-07-07 3:55 UTC (permalink / raw) To: Junio C Hamano; +Cc: Tay Ray Chuan, Git Mailing List, Shawn O. Pearce On 07/06/2011 07:31 PM, Junio C Hamano wrote: > Tay Ray Chuan <rctay89@gmail.com> writes: > >> On Thu, Jul 7, 2011 at 1:57 AM, Junio C Hamano <gitster@pobox.com> wrote: >>> Tay Ray Chuan <rctay89@gmail.com> writes: >>> >>>> Group free()'s that are called when a malloc() fails in >>>> xdl_prepare_ctx(), making for more readable code. >>> Nicer, but I wonder if we can get away with a single label that lets us >>> abort with freeing whatever we have allocated by making sure that the >>> various pointer fields and variables are initialized to NULL, which may >>> lead to even more readable code. >> Pardon my dullness. Do you mean to check if the various fields are set >> to NULL upon allocation, and free()'ing them if so? > What I meant was that, instead of doing this: > > func() { > if (somefunc(&A, ...) < 0) > goto failA; > ... do something ... > B = someotheralloc(); > if (!B) > goto failB; > ... do something ... > C = yetanotheralloc(); > if (!C) > goto failC; > ... do things using A, B, C ... > > return 0; > failC: > free(B); > failB: > free(A); > failA: > return -1; > } > > it would be easier to follow if you did: > > func() { > A = B = C = NULL; > if (somefunc(&A, ...) < 0) > goto fail; > ... do something ... > B = someotheralloc(); > if (!B) > goto fail; > ... do something ... > C = yetanotheralloc(); > if (!C) > goto fail; > ... do things using A, B, C ... > > fail: > free(B); > free(A); > return -1; > } > > Especially when you have more than 2 such fail labels. Because 'free(NULL)' is handled sanely (as a no-op). http://pubs.opengroup.org/onlinepubs/009695399/functions/free.html I haven't looked, but I assume xdl_cha_free does the same thing. I only assume this because Junio seems to imply it, though. Phil ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups 2011-07-07 3:55 ` Phil Hord @ 2011-07-07 4:05 ` Tay Ray Chuan 0 siblings, 0 replies; 16+ messages in thread From: Tay Ray Chuan @ 2011-07-07 4:05 UTC (permalink / raw) To: Phil Hord; +Cc: Junio C Hamano, Git Mailing List, Shawn O. Pearce On Thu, Jul 7, 2011 at 11:55 AM, Phil Hord <hordp@cisco.com> wrote: > Because 'free(NULL)' is handled sanely (as a no-op). > http://pubs.opengroup.org/onlinepubs/009695399/functions/free.html Yup, I got what Junio meant after he posted an example. (Thanks Junio.) > I haven't looked, but I assume xdl_cha_free does the same thing. I only > assume this because Junio seems to imply it, though. xdl_cha_(init|free)() do something more complicated, so we don't have to set xdf->cha to NULL, unlike the others. -- Cheers, Ray Chuan ^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v3 0/4] prepare for histogram diff 2011-07-06 16:38 [PATCH v2 0/4] prepare for histogram diff Tay Ray Chuan 2011-07-06 16:38 ` [PATCH v2 1/4] xdiff/xprepare: use memset() Tay Ray Chuan @ 2011-07-07 4:23 ` Tay Ray Chuan 2011-07-07 4:23 ` [PATCH v3 1/4] xdiff/xprepare: use memset() Tay Ray Chuan 1 sibling, 1 reply; 16+ messages in thread From: Tay Ray Chuan @ 2011-07-07 4:23 UTC (permalink / raw) To: Git Mailing List; +Cc: Junio C Hamano, Shawn O. Pearce Tay Ray Chuan (4): xdiff/xprepare: use memset() xdiff/xprepare: refactor abort cleanups xdiff/xpatience: factor out fall-back-diff function t4033-diff-patience: factor out tests t/lib-diff-alternative.sh | 165 +++++++++++++++++++++++++++++++++++++++++++++ t/t4033-diff-patience.sh | 162 +------------------------------------------- xdiff/xpatience.c | 27 +------- xdiff/xprepare.c | 101 ++++++++++------------------ xdiff/xutils.c | 31 +++++++++ xdiff/xutils.h | 2 + 6 files changed, 238 insertions(+), 250 deletions(-) create mode 100644 t/lib-diff-alternative.sh -- 1.7.3.4.676.gf08cd.dirty ^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v3 1/4] xdiff/xprepare: use memset() 2011-07-07 4:23 ` [PATCH v3 0/4] prepare for histogram diff Tay Ray Chuan @ 2011-07-07 4:23 ` Tay Ray Chuan 2011-07-07 4:23 ` [PATCH v3 2/4] xdiff/xprepare: refactor abort cleanups Tay Ray Chuan 0 siblings, 1 reply; 16+ messages in thread From: Tay Ray Chuan @ 2011-07-07 4:23 UTC (permalink / raw) To: Git Mailing List; +Cc: Junio C Hamano, Shawn O. Pearce Use memset() instead of a for loop to initialize. This could give a performance advantage. Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> --- xdiff/xprepare.c | 10 +++------- 1 files changed, 3 insertions(+), 7 deletions(-) diff --git a/xdiff/xprepare.c b/xdiff/xprepare.c index 1689085..783631a 100644 --- a/xdiff/xprepare.c +++ b/xdiff/xprepare.c @@ -64,8 +64,6 @@ static int xdl_optimize_ctxs(xdfile_t *xdf1, xdfile_t *xdf2); static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) { - long i; - cf->flags = flags; cf->hbits = xdl_hashbits((unsigned int) size); @@ -80,8 +78,7 @@ static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) { xdl_cha_free(&cf->ncha); return -1; } - for (i = 0; i < cf->hsize; i++) - cf->rchash[i] = NULL; + memset(cf->rchash, 0, cf->hsize * sizeof(xdlclass_t *)); cf->count = 0; @@ -136,7 +133,7 @@ static int xdl_classify_record(xdlclassifier_t *cf, xrecord_t **rhash, unsigned static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp, xdlclassifier_t *cf, xdfile_t *xdf) { unsigned int hbits; - long i, nrec, hsize, bsize; + long nrec, hsize, bsize; unsigned long hav; char const *blk, *cur, *top, *prev; xrecord_t *crec; @@ -164,8 +161,7 @@ static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp, xdl_cha_free(&xdf->rcha); return -1; } - for (i = 0; i < hsize; i++) - rhash[i] = NULL; + memset(rhash, 0, hsize * sizeof(xrecord_t *)); nrec = 0; if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) { -- 1.7.3.4.676.gf08cd.dirty ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v3 2/4] xdiff/xprepare: refactor abort cleanups 2011-07-07 4:23 ` [PATCH v3 1/4] xdiff/xprepare: use memset() Tay Ray Chuan @ 2011-07-07 4:23 ` Tay Ray Chuan 2011-07-07 4:23 ` [PATCH v3 3/4] xdiff/xpatience: factor out fall-back-diff function Tay Ray Chuan 0 siblings, 1 reply; 16+ messages in thread From: Tay Ray Chuan @ 2011-07-07 4:23 UTC (permalink / raw) To: Git Mailing List; +Cc: Junio C Hamano, Shawn O. Pearce Group free()'s that are called when a malloc() fails in xdl_prepare_ctx(), making for more readable code. Also add a free() on ha, in case future git hackers add allocs after the ha malloc. Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> --- Changed from v2: took up Junio's suggestion and used one cleanup label only. xdiff/xprepare.c | 91 +++++++++++++++++++----------------------------------- 1 files changed, 32 insertions(+), 59 deletions(-) diff --git a/xdiff/xprepare.c b/xdiff/xprepare.c index 783631a..0f571db 100644 --- a/xdiff/xprepare.c +++ b/xdiff/xprepare.c @@ -143,24 +143,21 @@ static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp, char *rchg; long *rindex; - if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) { + ha = NULL; + rindex = NULL; + rchg = NULL; + rhash = NULL; + recs = NULL; - return -1; - } - if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) { - - xdl_cha_free(&xdf->rcha); - return -1; - } + if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) + goto abort; + if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) + goto abort; hbits = xdl_hashbits((unsigned int) narec); hsize = 1 << hbits; - if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) { - - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; - } + if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) + goto abort; memset(rhash, 0, hsize * sizeof(xrecord_t *)); nrec = 0; @@ -175,63 +172,30 @@ static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp, hav = xdl_hash_record(&cur, top, xpp->flags); if (nrec >= narec) { narec *= 2; - if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) { - - xdl_free(rhash); - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; - } + if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) + goto abort; recs = rrecs; } - if (!(crec = xdl_cha_alloc(&xdf->rcha))) { - - xdl_free(rhash); - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; - } + if (!(crec = xdl_cha_alloc(&xdf->rcha))) + goto abort; crec->ptr = prev; crec->size = (long) (cur - prev); crec->ha = hav; recs[nrec++] = crec; - if (xdl_classify_record(cf, rhash, hbits, crec) < 0) { - - xdl_free(rhash); - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; - } + if (xdl_classify_record(cf, rhash, hbits, crec) < 0) + goto abort; } } - if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) { - - xdl_free(rhash); - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; - } + if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) + goto abort; memset(rchg, 0, (nrec + 2) * sizeof(char)); - if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) { - - xdl_free(rchg); - xdl_free(rhash); - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; - } - if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) { - - xdl_free(rindex); - xdl_free(rchg); - xdl_free(rhash); - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; - } + if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) + goto abort; + if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) + goto abort; xdf->nrec = nrec; xdf->recs = recs; @@ -245,6 +209,15 @@ static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp, xdf->dend = nrec - 1; return 0; + +abort: + xdl_free(ha); + xdl_free(rindex); + xdl_free(rchg); + xdl_free(rhash); + xdl_free(recs); + xdl_cha_free(&xdf->rcha); + return -1; } -- 1.7.3.4.676.gf08cd.dirty ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v3 3/4] xdiff/xpatience: factor out fall-back-diff function 2011-07-07 4:23 ` [PATCH v3 2/4] xdiff/xprepare: refactor abort cleanups Tay Ray Chuan @ 2011-07-07 4:23 ` Tay Ray Chuan 2011-07-07 4:23 ` [PATCH v3 4/4] t4033-diff-patience: factor out tests Tay Ray Chuan 0 siblings, 1 reply; 16+ messages in thread From: Tay Ray Chuan @ 2011-07-07 4:23 UTC (permalink / raw) To: Git Mailing List; +Cc: Junio C Hamano, Shawn O. Pearce This is in preparation for the histogram diff algorithm, which will also re-use much of the code to call the default Meyers diff algorithm. Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> --- xdiff/xpatience.c | 27 ++------------------------- xdiff/xutils.c | 31 +++++++++++++++++++++++++++++++ xdiff/xutils.h | 2 ++ 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/xdiff/xpatience.c b/xdiff/xpatience.c index e42c16a..fdd7d02 100644 --- a/xdiff/xpatience.c +++ b/xdiff/xpatience.c @@ -287,34 +287,11 @@ static int walk_common_sequence(struct hashmap *map, struct entry *first, static int fall_back_to_classic_diff(struct hashmap *map, int line1, int count1, int line2, int count2) { - /* - * This probably does not work outside Git, since - * we have a very simple mmfile structure. - * - * Note: ideally, we would reuse the prepared environment, but - * the libxdiff interface does not (yet) allow for diffing only - * ranges of lines instead of the whole files. - */ - mmfile_t subfile1, subfile2; xpparam_t xpp; - xdfenv_t env; - - subfile1.ptr = (char *)map->env->xdf1.recs[line1 - 1]->ptr; - subfile1.size = map->env->xdf1.recs[line1 + count1 - 2]->ptr + - map->env->xdf1.recs[line1 + count1 - 2]->size - subfile1.ptr; - subfile2.ptr = (char *)map->env->xdf2.recs[line2 - 1]->ptr; - subfile2.size = map->env->xdf2.recs[line2 + count2 - 2]->ptr + - map->env->xdf2.recs[line2 + count2 - 2]->size - subfile2.ptr; xpp.flags = map->xpp->flags & ~XDF_PATIENCE_DIFF; - if (xdl_do_diff(&subfile1, &subfile2, &xpp, &env) < 0) - return -1; - memcpy(map->env->xdf1.rchg + line1 - 1, env.xdf1.rchg, count1); - memcpy(map->env->xdf2.rchg + line2 - 1, env.xdf2.rchg, count2); - - xdl_free_env(&env); - - return 0; + return xdl_fall_back_diff(map->env, &xpp, + line1, count1, line2, count2); } /* diff --git a/xdiff/xutils.c b/xdiff/xutils.c index ab65034..ded7c32 100644 --- a/xdiff/xutils.c +++ b/xdiff/xutils.c @@ -402,3 +402,34 @@ int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, return 0; } + +int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp, + int line1, int count1, int line2, int count2) +{ + /* + * This probably does not work outside Git, since + * we have a very simple mmfile structure. + * + * Note: ideally, we would reuse the prepared environment, but + * the libxdiff interface does not (yet) allow for diffing only + * ranges of lines instead of the whole files. + */ + mmfile_t subfile1, subfile2; + xdfenv_t env; + + subfile1.ptr = (char *)diff_env->xdf1.recs[line1 - 1]->ptr; + subfile1.size = diff_env->xdf1.recs[line1 + count1 - 2]->ptr + + diff_env->xdf1.recs[line1 + count1 - 2]->size - subfile1.ptr; + subfile2.ptr = (char *)diff_env->xdf2.recs[line2 - 1]->ptr; + subfile2.size = diff_env->xdf2.recs[line2 + count2 - 2]->ptr + + diff_env->xdf2.recs[line2 + count2 - 2]->size - subfile2.ptr; + if (xdl_do_diff(&subfile1, &subfile2, xpp, &env) < 0) + return -1; + + memcpy(diff_env->xdf1.rchg + line1 - 1, env.xdf1.rchg, count1); + memcpy(diff_env->xdf2.rchg + line2 - 1, env.xdf2.rchg, count2); + + xdl_free_env(&env); + + return 0; +} diff --git a/xdiff/xutils.h b/xdiff/xutils.h index d5de829..674a657 100644 --- a/xdiff/xutils.h +++ b/xdiff/xutils.h @@ -41,6 +41,8 @@ int xdl_num_out(char *out, long val); long xdl_atol(char const *str, char const **next); int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, const char *func, long funclen, xdemitcb_t *ecb); +int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp, + int line1, int count1, int line2, int count2); -- 1.7.3.4.676.gf08cd.dirty ^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v3 4/4] t4033-diff-patience: factor out tests 2011-07-07 4:23 ` [PATCH v3 3/4] xdiff/xpatience: factor out fall-back-diff function Tay Ray Chuan @ 2011-07-07 4:23 ` Tay Ray Chuan 0 siblings, 0 replies; 16+ messages in thread From: Tay Ray Chuan @ 2011-07-07 4:23 UTC (permalink / raw) To: Git Mailing List; +Cc: Junio C Hamano, Shawn O. Pearce Group the test cases into two functions, test_diff_(frobnitz|unique). This in preparation for the histogram diff algorithm, which would also re-use these test cases. Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> --- Changed from v2: refactored tests live in lib-diff-alternative.sh instead of lib-diff-patience.sh. t/lib-diff-alternative.sh | 165 +++++++++++++++++++++++++++++++++++++++++++++ t/t4033-diff-patience.sh | 162 +------------------------------------------- 2 files changed, 168 insertions(+), 159 deletions(-) create mode 100644 t/lib-diff-alternative.sh diff --git a/t/lib-diff-alternative.sh b/t/lib-diff-alternative.sh new file mode 100644 index 0000000..75ffd91 --- /dev/null +++ b/t/lib-diff-alternative.sh @@ -0,0 +1,165 @@ +#!/bin/sh + +test_diff_frobnitz() { + cat >file1 <<\EOF +#include <stdio.h> + +// Frobs foo heartily +int frobnitz(int foo) +{ + int i; + for(i = 0; i < 10; i++) + { + printf("Your answer is: "); + printf("%d\n", foo); + } +} + +int fact(int n) +{ + if(n > 1) + { + return fact(n-1) * n; + } + return 1; +} + +int main(int argc, char **argv) +{ + frobnitz(fact(10)); +} +EOF + + cat >file2 <<\EOF +#include <stdio.h> + +int fib(int n) +{ + if(n > 2) + { + return fib(n-1) + fib(n-2); + } + return 1; +} + +// Frobs foo heartily +int frobnitz(int foo) +{ + int i; + for(i = 0; i < 10; i++) + { + printf("%d\n", foo); + } +} + +int main(int argc, char **argv) +{ + frobnitz(fib(10)); +} +EOF + + cat >expect <<\EOF +diff --git a/file1 b/file2 +index 6faa5a3..e3af329 100644 +--- a/file1 ++++ b/file2 +@@ -1,26 +1,25 @@ + #include <stdio.h> + ++int fib(int n) ++{ ++ if(n > 2) ++ { ++ return fib(n-1) + fib(n-2); ++ } ++ return 1; ++} ++ + // Frobs foo heartily + int frobnitz(int foo) + { + int i; + for(i = 0; i < 10; i++) + { +- printf("Your answer is: "); + printf("%d\n", foo); + } + } + +-int fact(int n) +-{ +- if(n > 1) +- { +- return fact(n-1) * n; +- } +- return 1; +-} +- + int main(int argc, char **argv) + { +- frobnitz(fact(10)); ++ frobnitz(fib(10)); + } +EOF + + STRATEGY=$1 + + test_expect_success "$STRATEGY diff" ' + test_must_fail git diff --no-index "--$STRATEGY" file1 file2 > output && + test_cmp expect output + ' + + test_expect_success "$STRATEGY diff output is valid" ' + mv file2 expect && + git apply < output && + test_cmp expect file2 + ' +} + +test_diff_unique() { + cat >uniq1 <<\EOF +1 +2 +3 +4 +5 +6 +EOF + + cat >uniq2 <<\EOF +a +b +c +d +e +f +EOF + + cat >expect <<\EOF +diff --git a/uniq1 b/uniq2 +index b414108..0fdf397 100644 +--- a/uniq1 ++++ b/uniq2 +@@ -1,6 +1,6 @@ +-1 +-2 +-3 +-4 +-5 +-6 ++a ++b ++c ++d ++e ++f +EOF + + STRATEGY=$1 + + test_expect_success 'completely different files' ' + test_must_fail git diff --no-index "--$STRATEGY" uniq1 uniq2 > output && + test_cmp expect output + ' +} + diff --git a/t/t4033-diff-patience.sh b/t/t4033-diff-patience.sh index 1eb1498..3c9932e 100755 --- a/t/t4033-diff-patience.sh +++ b/t/t4033-diff-patience.sh @@ -3,166 +3,10 @@ test_description='patience diff algorithm' . ./test-lib.sh +. "$TEST_DIRECTORY"/lib-diff-alternative.sh -cat >file1 <<\EOF -#include <stdio.h> +test_diff_frobnitz "patience" -// Frobs foo heartily -int frobnitz(int foo) -{ - int i; - for(i = 0; i < 10; i++) - { - printf("Your answer is: "); - printf("%d\n", foo); - } -} - -int fact(int n) -{ - if(n > 1) - { - return fact(n-1) * n; - } - return 1; -} - -int main(int argc, char **argv) -{ - frobnitz(fact(10)); -} -EOF - -cat >file2 <<\EOF -#include <stdio.h> - -int fib(int n) -{ - if(n > 2) - { - return fib(n-1) + fib(n-2); - } - return 1; -} - -// Frobs foo heartily -int frobnitz(int foo) -{ - int i; - for(i = 0; i < 10; i++) - { - printf("%d\n", foo); - } -} - -int main(int argc, char **argv) -{ - frobnitz(fib(10)); -} -EOF - -cat >expect <<\EOF -diff --git a/file1 b/file2 -index 6faa5a3..e3af329 100644 ---- a/file1 -+++ b/file2 -@@ -1,26 +1,25 @@ - #include <stdio.h> - -+int fib(int n) -+{ -+ if(n > 2) -+ { -+ return fib(n-1) + fib(n-2); -+ } -+ return 1; -+} -+ - // Frobs foo heartily - int frobnitz(int foo) - { - int i; - for(i = 0; i < 10; i++) - { -- printf("Your answer is: "); - printf("%d\n", foo); - } - } - --int fact(int n) --{ -- if(n > 1) -- { -- return fact(n-1) * n; -- } -- return 1; --} -- - int main(int argc, char **argv) - { -- frobnitz(fact(10)); -+ frobnitz(fib(10)); - } -EOF - -test_expect_success 'patience diff' ' - - test_must_fail git diff --no-index --patience file1 file2 > output && - test_cmp expect output - -' - -test_expect_success 'patience diff output is valid' ' - - mv file2 expect && - git apply < output && - test_cmp expect file2 - -' - -cat >uniq1 <<\EOF -1 -2 -3 -4 -5 -6 -EOF - -cat >uniq2 <<\EOF -a -b -c -d -e -f -EOF - -cat >expect <<\EOF -diff --git a/uniq1 b/uniq2 -index b414108..0fdf397 100644 ---- a/uniq1 -+++ b/uniq2 -@@ -1,6 +1,6 @@ --1 --2 --3 --4 --5 --6 -+a -+b -+c -+d -+e -+f -EOF - -test_expect_success 'completely different files' ' - - test_must_fail git diff --no-index --patience uniq1 uniq2 > output && - test_cmp expect output - -' +test_diff_unique "patience" test_done -- 1.7.3.4.676.gf08cd.dirty ^ permalink raw reply related [flat|nested] 16+ messages in thread
end of thread, other threads:[~2011-07-07 4:24 UTC | newest] Thread overview: 16+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2011-07-06 16:38 [PATCH v2 0/4] prepare for histogram diff Tay Ray Chuan 2011-07-06 16:38 ` [PATCH v2 1/4] xdiff/xprepare: use memset() Tay Ray Chuan 2011-07-06 16:38 ` [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups Tay Ray Chuan 2011-07-06 16:38 ` [PATCH v2 3/4] xdiff/xpatience: factor out fall-back-diff function Tay Ray Chuan 2011-07-06 16:38 ` [PATCH v2 4/4] t4033-diff-patience: factor out tests Tay Ray Chuan 2011-07-06 18:04 ` Junio C Hamano 2011-07-06 17:57 ` [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups Junio C Hamano 2011-07-06 18:10 ` Tay Ray Chuan 2011-07-06 23:31 ` Junio C Hamano 2011-07-07 3:55 ` Phil Hord 2011-07-07 4:05 ` Tay Ray Chuan 2011-07-07 4:23 ` [PATCH v3 0/4] prepare for histogram diff Tay Ray Chuan 2011-07-07 4:23 ` [PATCH v3 1/4] xdiff/xprepare: use memset() Tay Ray Chuan 2011-07-07 4:23 ` [PATCH v3 2/4] xdiff/xprepare: refactor abort cleanups Tay Ray Chuan 2011-07-07 4:23 ` [PATCH v3 3/4] xdiff/xpatience: factor out fall-back-diff function Tay Ray Chuan 2011-07-07 4:23 ` [PATCH v3 4/4] t4033-diff-patience: factor out tests Tay Ray Chuan
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).