Blame tests/check_list.c

Packit 0b5880
/*
Packit 0b5880
 * Check: a unit test framework for C
Packit 0b5880
 * Copyright (C) 2001, 2002 Arien Malec
Packit 0b5880
 *
Packit 0b5880
 * This library is free software; you can redistribute it and/or
Packit 0b5880
 * modify it under the terms of the GNU Lesser General Public
Packit 0b5880
 * License as published by the Free Software Foundation; either
Packit 0b5880
 * version 2.1 of the License, or (at your option) any later version.
Packit 0b5880
 *
Packit 0b5880
 * This library is distributed in the hope that it will be useful,
Packit 0b5880
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0b5880
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0b5880
 * Lesser General Public License for more details.
Packit 0b5880
 *
Packit 0b5880
 * You should have received a copy of the GNU Lesser General Public
Packit 0b5880
 * License along with this library; if not, write to the
Packit 0b5880
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
Packit 0b5880
 * MA 02110-1301, USA.
Packit 0b5880
 */
Packit 0b5880
Packit 0b5880
#include "../lib/libcompat.h"
Packit 0b5880
Packit 0b5880
#include <string.h>
Packit 0b5880
#include <stdlib.h>
Packit 0b5880
Packit 0b5880
#include "check.h"
Packit 0b5880
#include "check_list.h"
Packit 0b5880
#include "check_check.h"
Packit 0b5880
Packit 0b5880
START_TEST(test_create)
Packit 0b5880
{
Packit 0b5880
  List *lp = NULL;
Packit 0b5880
Packit 0b5880
  ck_assert_msg (check_list_val(lp) == NULL,
Packit 0b5880
	       "Current list value should be NULL for NULL list");
Packit 0b5880
Packit 0b5880
  lp = check_list_create();
Packit 0b5880
Packit 0b5880
  ck_assert_msg (check_list_val(lp) == NULL,
Packit 0b5880
	       "Current list value should be NULL for newly created list");
Packit 0b5880
Packit 0b5880
  ck_assert_msg (check_list_at_end(lp),
Packit 0b5880
	       "Newly created list should be at end");
Packit 0b5880
  check_list_advance(lp);
Packit 0b5880
  ck_assert_msg (check_list_at_end(lp),
Packit 0b5880
	       "Advancing a list at end should produce a list at end");
Packit 0b5880
  check_list_free (lp);
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_free)
Packit 0b5880
{
Packit 0b5880
  List *lp = check_list_create();
Packit 0b5880
  char data_abc[] = "abc";
Packit 0b5880
  char data_123[] = "123";
Packit 0b5880
Packit 0b5880
  check_list_add_end (lp, data_abc);
Packit 0b5880
  check_list_add_end (lp, data_123);
Packit 0b5880
  check_list_add_end (lp, NULL);
Packit 0b5880
  check_list_free (lp);
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_add_end)
Packit 0b5880
{
Packit 0b5880
  List * lp = check_list_create();
Packit 0b5880
  char tval[] = "abc";
Packit 0b5880
  
Packit 0b5880
  check_list_add_end (lp, tval);
Packit 0b5880
  
Packit 0b5880
  ck_assert_msg (check_list_val (lp) != NULL,
Packit 0b5880
	       "List current val should not be null after new insertion");
Packit 0b5880
  ck_assert_msg (!check_list_at_end (lp),
Packit 0b5880
	       "List should be at end after new insertion");
Packit 0b5880
  ck_assert_msg (strcmp(tval, (char *) check_list_val (lp)) == 0,
Packit 0b5880
	       "List current val should equal newly inserted val");
Packit 0b5880
  check_list_free (lp);
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_add_front)
Packit 0b5880
{
Packit 0b5880
  List * lp = check_list_create();
Packit 0b5880
  char tval[] = "abc";
Packit 0b5880
  
Packit 0b5880
  check_list_add_front (lp, tval);
Packit 0b5880
  
Packit 0b5880
  ck_assert_msg (check_list_val (lp) != NULL,
Packit 0b5880
	       "List current val should not be null after new insertion");
Packit 0b5880
  ck_assert_msg (strcmp(tval, (char *) check_list_val (lp)) == 0,
Packit 0b5880
	       "List current val should equal newly inserted val");
Packit 0b5880
  check_list_free (lp);
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_add_end_and_next)
Packit 0b5880
{
Packit 0b5880
  List *lp = check_list_create();
Packit 0b5880
  char tval1[] = "abc";
Packit 0b5880
  char tval2[] = "123";
Packit 0b5880
  
Packit 0b5880
  check_list_add_end (lp, tval1);
Packit 0b5880
  check_list_add_end (lp, tval2);
Packit 0b5880
  check_list_front(lp);
Packit 0b5880
  ck_assert_msg (strcmp (tval1, (char *)check_list_val (lp)) == 0,
Packit 0b5880
	       "List head val should equal first inserted val");
Packit 0b5880
  check_list_advance (lp);
Packit 0b5880
  ck_assert_msg (!check_list_at_end (lp),
Packit 0b5880
	       "List should not be at end after two adds and one next");
Packit 0b5880
  ck_assert_msg (strcmp (tval2, (char *)check_list_val (lp)) == 0,
Packit 0b5880
	       "List val should equal second inserted val");
Packit 0b5880
  check_list_advance(lp);
Packit 0b5880
  ck_assert_msg (check_list_at_end (lp),
Packit 0b5880
	       "List should be at and after two adds and two nexts");
Packit 0b5880
  check_list_free (lp);
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
Packit 0b5880
START_TEST(test_add_front_and_next)
Packit 0b5880
{
Packit 0b5880
  List * lp = check_list_create();
Packit 0b5880
  char tval1[] = "abc";
Packit 0b5880
  char tval2[] = "123";
Packit 0b5880
  
Packit 0b5880
  check_list_add_front (lp, tval1);
Packit 0b5880
  check_list_add_front (lp, tval2);
Packit 0b5880
  check_list_front(lp);
Packit 0b5880
  ck_assert_msg (strcmp (tval2, (char *)check_list_val (lp)) == 0,
Packit 0b5880
	       "List head val should equal last inserted val");
Packit 0b5880
  check_list_advance (lp);
Packit 0b5880
  ck_assert_msg (!check_list_at_end (lp),
Packit 0b5880
	       "List should not be at end after two adds and one next");
Packit 0b5880
  ck_assert_msg (strcmp (tval1, (char *)check_list_val (lp)) == 0,
Packit 0b5880
	       "List val should equal first inserted val");
Packit 0b5880
  check_list_advance(lp);
Packit 0b5880
  ck_assert_msg (check_list_at_end (lp),
Packit 0b5880
	       "List should be at and after two adds and two nexts");
Packit 0b5880
  check_list_free (lp);
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_add_a_bunch)
Packit 0b5880
{
Packit 0b5880
  List *lp;
Packit 0b5880
  int i, j;
Packit 0b5880
  char tval1[] = "abc";
Packit 0b5880
  char tval2[] = "123";
Packit 0b5880
  for (i = 0; i < 3; i++) {
Packit 0b5880
    lp = check_list_create();
Packit 0b5880
    for (j = 0; j < 1000; j++) {
Packit 0b5880
      check_list_add_end (lp, tval1);
Packit 0b5880
      check_list_add_front (lp, tval2);
Packit 0b5880
    }
Packit 0b5880
    check_list_free(lp);
Packit 0b5880
  }
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_list_abuse)
Packit 0b5880
{
Packit 0b5880
    check_list_advance(NULL);
Packit 0b5880
    /* Should not crash */
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_contains)
Packit 0b5880
{
Packit 0b5880
    List *lp = check_list_create();
Packit 0b5880
Packit 0b5880
    char otherData[] = "other";
Packit 0b5880
    char goalData[] = "goal";
Packit 0b5880
    int index;
Packit 0b5880
Packit 0b5880
    ck_assert_msg (check_list_contains(lp, goalData) == 0,
Packit 0b5880
                       "The goal data should not be in the list yet");
Packit 0b5880
Packit 0b5880
    for(index = 0; index < 10; index++)
Packit 0b5880
    {
Packit 0b5880
        check_list_add_end (lp, otherData);
Packit 0b5880
        ck_assert_msg (check_list_contains(lp, goalData) == 0,
Packit 0b5880
                   "The goal data should not be in the list yet");
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    check_list_add_end (lp, goalData);
Packit 0b5880
    ck_assert_msg (check_list_contains(lp, goalData) ,
Packit 0b5880
                       "The goal data should be in the list");
Packit 0b5880
Packit 0b5880
    check_list_free(lp);
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
Suite *make_list_suite (void)
Packit 0b5880
{
Packit 0b5880
  Suite *s = suite_create("Lists");
Packit 0b5880
  TCase * tc = tcase_create("Core");
Packit 0b5880
Packit 0b5880
  suite_add_tcase (s, tc);
Packit 0b5880
  tcase_add_test (tc, test_create);
Packit 0b5880
  tcase_add_test (tc, test_free);
Packit 0b5880
  tcase_add_test (tc, test_add_end);
Packit 0b5880
  tcase_add_test (tc, test_add_front);
Packit 0b5880
  tcase_add_test (tc, test_add_end_and_next);
Packit 0b5880
  tcase_add_test (tc, test_add_front_and_next);
Packit 0b5880
  tcase_add_test (tc, test_add_a_bunch);
Packit 0b5880
  tcase_add_test (tc, test_list_abuse);
Packit 0b5880
  tcase_add_test (tc, test_contains);
Packit 0b5880
Packit 0b5880
  return s;
Packit 0b5880
}