Changeset 25891a8 for tester.c


Ignore:
Timestamp:
Jun 25, 2011, 6:42:19 AM (13 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.10, release-1.8, release-1.9
Children:
b2bfe1f, af562f5, b8a3e00, e1d3607, 16ce7a4
Parents:
650fb2c
git-author:
David Benjamin <davidben@mit.edu> (06/25/11 04:32:18)
git-committer:
David Benjamin <davidben@mit.edu> (06/25/11 06:42:19)
Message:
Add a unit test for owl_history

Also fix an off-by-one error in bounding the history size. May as well
have those semantics be coherent and testable.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tester.c

    rce68f23 r25891a8  
    2323int owl_fmtext_regtest(void);
    2424int owl_smartfilter_regtest(void);
     25int owl_history_regtest(void);
    2526
    2627extern void owl_perl_xs_init(pTHX);
     
    112113  numfailures += owl_fmtext_regtest();
    113114  numfailures += owl_smartfilter_regtest();
     115  numfailures += owl_history_regtest();
    114116  if (numfailures) {
    115117      fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures);
     
    803805  return numfailed;
    804806}
     807
     808int owl_history_regtest(void)
     809{
     810  int numfailed = 0;
     811  int i;
     812  owl_history h;
     813
     814  printf("# BEGIN testing owl_history\n");
     815  owl_history_init(&h);
     816
     817  /* Operations on empty history. */
     818  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
     819  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
     820  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
     821
     822  /* Insert a few records. */
     823  owl_history_store(&h, "a", false);
     824  owl_history_store(&h, "b", false);
     825  owl_history_store(&h, "c", false);
     826  owl_history_store(&h, "d", true);
     827
     828  /* Walk up and down the history a bit. */
     829  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
     830  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
     831  FAIL_UNLESS("touched", owl_history_is_touched(&h));
     832  FAIL_UNLESS("next d", strcmp(owl_history_get_next(&h), "d") == 0);
     833  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
     834  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
     835  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
     836  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
     837  FAIL_UNLESS("prev a", strcmp(owl_history_get_prev(&h), "a") == 0);
     838  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
     839
     840  /* Now insert something. It should reset and blow away 'd'. */
     841  owl_history_store(&h, "e", false);
     842  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
     843  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
     844  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
     845  FAIL_UNLESS("touched", owl_history_is_touched(&h));
     846  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
     847  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
     848
     849  /* Lines get de-duplicated on insert. */
     850  owl_history_store(&h, "e", false);
     851  owl_history_store(&h, "e", false);
     852  owl_history_store(&h, "e", false);
     853  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
     854  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
     855
     856  /* But a partial is not deduplicated, as it'll go away soon. */
     857  owl_history_store(&h, "e", true);
     858  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
     859  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
     860  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
     861  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
     862
     863  /* Reset moves to the front... */
     864  owl_history_store(&h, "f", true);
     865  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
     866  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
     867  owl_history_reset(&h);
     868  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
     869  /* ...and destroys any pending partial entry... */
     870  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
     871  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
     872  /* ...but not non-partial ones. */
     873  owl_history_reset(&h);
     874  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
     875
     876  /* Finally, check we are bounded by OWL_HISTORYSIZE. */
     877  for (i = 0; i < OWL_HISTORYSIZE; i++) {
     878    char *string = g_strdup_printf("mango%d", i);
     879    owl_history_store(&h, string, false);
     880    g_free(string);
     881  }
     882  /* The OWL_HISTORYSIZE'th prev gets NULL. */
     883  for (i = OWL_HISTORYSIZE - 2; i >= 0; i--) {
     884    char *string = g_strdup_printf("mango%d", i);
     885    FAIL_UNLESS("prev mango_N", strcmp(owl_history_get_prev(&h), string) == 0);
     886    g_free(string);
     887  }
     888  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
     889
     890  owl_history_cleanup(&h);
     891
     892  printf("# END testing owl_history (%d failures)\n", numfailed);
     893  return numfailed;
     894}
Note: See TracChangeset for help on using the changeset viewer.