Blame tests/check_check_selective.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 <stdio.h>
Packit 0b5880
#include <stdlib.h>
Packit 0b5880
#include <check.h>
Packit 0b5880
#include "check_check.h"
Packit 0b5880
Packit 0b5880
static SRunner *sr;
Packit 0b5880
static int test_tc11_executed;
Packit 0b5880
static int test_tc12_executed;
Packit 0b5880
static int test_tc21_executed;
Packit 0b5880
Packit 0b5880
static void reset_executed (void)
Packit 0b5880
{
Packit 0b5880
  test_tc11_executed = 0;
Packit 0b5880
  test_tc12_executed = 0;
Packit 0b5880
  test_tc21_executed = 0;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
START_TEST(test_tc11)
Packit 0b5880
{
Packit 0b5880
  test_tc11_executed = 1;
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_tc12)
Packit 0b5880
{
Packit 0b5880
  test_tc12_executed = 1;
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_tc21)
Packit 0b5880
{
Packit 0b5880
  test_tc21_executed = 1;
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
static void selective_setup (void)
Packit 0b5880
{
Packit 0b5880
  Suite *s1, *s2;
Packit 0b5880
  TCase *tc11, *tc12, *tc21;
Packit 0b5880
Packit 0b5880
  /*
Packit 0b5880
   * Create a test suite 'suite1' with two test cases 'tcase11' and
Packit 0b5880
   * 'tcase12' containing a single test each.
Packit 0b5880
   */
Packit 0b5880
  s1 = suite_create ("suite1");
Packit 0b5880
  tc11 = tcase_create ("tcase11");
Packit 0b5880
  tcase_add_test (tc11, test_tc11);
Packit 0b5880
  tc12 = tcase_create ("tcase12");
Packit 0b5880
  tcase_add_test (tc12, test_tc12);
Packit 0b5880
  suite_add_tcase (s1, tc11);
Packit 0b5880
  suite_add_tcase (s1, tc12);
Packit 0b5880
  
Packit 0b5880
  /* This line intentionally attempts to add an already
Packit 0b5880
   * added test case twice, to ensure it is not added
Packit 0b5880
   * again. If it was added again, when the test cases
Packit 0b5880
   * are freed a double-free failure will occur. */
Packit 0b5880
  suite_add_tcase (s1, tc12);
Packit 0b5880
Packit 0b5880
  /*
Packit 0b5880
   * Create a test suite 'suite2' with one test case 'test21'
Packit 0b5880
   * containing two tests.
Packit 0b5880
   */
Packit 0b5880
  s2 = suite_create ("suite2");
Packit 0b5880
  tc21 = tcase_create ("tcase21");
Packit 0b5880
  tcase_add_test (tc21, test_tc21);
Packit 0b5880
  suite_add_tcase (s2, tc21);
Packit 0b5880
Packit 0b5880
  sr = srunner_create (s1);
Packit 0b5880
  srunner_add_suite (sr, s2);
Packit 0b5880
  srunner_set_fork_status (sr, CK_NOFORK);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void selective_teardown (void)
Packit 0b5880
{
Packit 0b5880
  srunner_free (sr);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
Packit 0b5880
START_TEST(test_srunner_run_run_all)
Packit 0b5880
{
Packit 0b5880
  /* This test exercises the srunner_run function for the case where
Packit 0b5880
     both sname and tcname are NULL.  That means to run all the test
Packit 0b5880
     cases in all the defined suites.  */
Packit 0b5880
  srunner_run (sr,
Packit 0b5880
               NULL, /* NULL tsuite name.  */
Packit 0b5880
               NULL, /* NULL tcase name.  */
Packit 0b5880
               CK_VERBOSE);
Packit 0b5880
Packit 0b5880
  ck_assert_msg (srunner_ntests_run(sr) == 3,
Packit 0b5880
               "Not all tests were executed.");
Packit 0b5880
Packit 0b5880
  reset_executed ();
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_srunner_run_suite)
Packit 0b5880
{
Packit 0b5880
  /* This test makes the srunner_run function to run all the test
Packit 0b5880
     cases of a existing suite.  */
Packit 0b5880
  srunner_run (sr,
Packit 0b5880
               "suite1",
Packit 0b5880
               NULL,  /* NULL tcase name.  */
Packit 0b5880
               CK_VERBOSE);
Packit 0b5880
Packit 0b5880
  ck_assert_msg (test_tc11_executed 
Packit 0b5880
               && test_tc12_executed
Packit 0b5880
               && !test_tc21_executed,
Packit 0b5880
               "Expected tests were not executed.");
Packit 0b5880
Packit 0b5880
  reset_executed ();
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_srunner_run_no_suite)
Packit 0b5880
{
Packit 0b5880
  /* This test makes the srunner_run function to run all the test
Packit 0b5880
     cases of a non-existing suite.  */
Packit 0b5880
  srunner_run (sr,
Packit 0b5880
               "non-existing-suite",
Packit 0b5880
               NULL, /* NULL tcase name. */
Packit 0b5880
               CK_VERBOSE);
Packit 0b5880
  
Packit 0b5880
  ck_assert_msg (!(test_tc11_executed
Packit 0b5880
           || test_tc12_executed
Packit 0b5880
           || test_tc21_executed),
Packit 0b5880
           "An unexpected test was executed.");
Packit 0b5880
Packit 0b5880
  reset_executed ();
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_srunner_run_tcase)
Packit 0b5880
{
Packit 0b5880
  /* This test makes the srunner_run function to run an specific
Packit 0b5880
     existing test case.  */
Packit 0b5880
  srunner_run (sr,
Packit 0b5880
               NULL,  /* NULL suite name.  */
Packit 0b5880
               "tcase12",
Packit 0b5880
               CK_VERBOSE);
Packit 0b5880
Packit 0b5880
  ck_assert_msg (!test_tc11_executed
Packit 0b5880
               && test_tc12_executed
Packit 0b5880
               && !test_tc21_executed,
Packit 0b5880
               "Expected tests were not executed.");
Packit 0b5880
  
Packit 0b5880
  reset_executed ();
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_srunner_no_tcase)
Packit 0b5880
{
Packit 0b5880
  /* This test makes the srunner_run function to run a non-existant
Packit 0b5880
     test case.  */
Packit 0b5880
  srunner_run (sr,
Packit 0b5880
               NULL,  /* NULL suite name.  */
Packit 0b5880
               "non-existant-test-case",
Packit 0b5880
               CK_VERBOSE);
Packit 0b5880
Packit 0b5880
  ck_assert_msg (!(test_tc11_executed
Packit 0b5880
           || test_tc12_executed
Packit 0b5880
           || test_tc21_executed),
Packit 0b5880
           "An unexpected test was executed.");
Packit 0b5880
Packit 0b5880
  reset_executed ();
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_srunner_suite_tcase)
Packit 0b5880
{
Packit 0b5880
  /* This test makes the srunner_run function to run a specific test
Packit 0b5880
     case of a specific test suite.  */
Packit 0b5880
  srunner_run (sr,
Packit 0b5880
               "suite2",
Packit 0b5880
               "tcase21",
Packit 0b5880
               CK_VERBOSE);
Packit 0b5880
  
Packit 0b5880
  ck_assert_msg (!test_tc11_executed
Packit 0b5880
               && !test_tc12_executed
Packit 0b5880
               && test_tc21_executed,
Packit 0b5880
               "Expected tests were not executed.");
Packit 0b5880
Packit 0b5880
  reset_executed ();
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_srunner_suite_no_tcase)
Packit 0b5880
{
Packit 0b5880
  /* This test makes the srunner_run function to run a non existant
Packit 0b5880
     test case of a specific test suite.  */
Packit 0b5880
  srunner_run (sr,
Packit 0b5880
               "suite1",
Packit 0b5880
               "non-existant-test-case",
Packit 0b5880
               CK_VERBOSE);
Packit 0b5880
Packit 0b5880
  ck_assert_msg (!(test_tc11_executed
Packit 0b5880
           || test_tc12_executed
Packit 0b5880
           || test_tc21_executed),
Packit 0b5880
           "An unexpected test was executed.");
Packit 0b5880
Packit 0b5880
  reset_executed ();
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
Packit 0b5880
#if HAVE_DECL_SETENV
Packit 0b5880
START_TEST(test_srunner_run_suite_env)
Packit 0b5880
{
Packit 0b5880
  /* This test makes the srunner_run_all function to run all the test
Packit 0b5880
     cases of a existing suite.  */
Packit 0b5880
  setenv ("CK_RUN_SUITE", "suite1", 1);
Packit 0b5880
  srunner_run_all (sr, CK_VERBOSE);
Packit 0b5880
Packit 0b5880
  ck_assert_msg (test_tc11_executed 
Packit 0b5880
               && test_tc12_executed
Packit 0b5880
               && !test_tc21_executed,
Packit 0b5880
               "Expected tests were not executed.");
Packit 0b5880
Packit 0b5880
  reset_executed ();
Packit 0b5880
  unsetenv ("CK_RUN_SUITE");
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_srunner_run_no_suite_env)
Packit 0b5880
{
Packit 0b5880
  /* This test makes the srunner_run_all function to run all the test
Packit 0b5880
     cases of a non-existing suite.  */
Packit 0b5880
  setenv ("CK_RUN_SUITE", "non-existing-suite", 1);
Packit 0b5880
  srunner_run_all (sr, CK_VERBOSE);
Packit 0b5880
  
Packit 0b5880
  ck_assert_msg (!(test_tc11_executed
Packit 0b5880
           || test_tc12_executed
Packit 0b5880
           || test_tc21_executed),
Packit 0b5880
           "An unexpected test was executed.");
Packit 0b5880
Packit 0b5880
  reset_executed ();
Packit 0b5880
  unsetenv ("CK_RUN_SUITE");
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_srunner_run_tcase_env)
Packit 0b5880
{
Packit 0b5880
  /* This test makes the srunner_run_all function to run an specific
Packit 0b5880
     existing test case.  */
Packit 0b5880
  setenv ("CK_RUN_CASE", "tcase12", 1);
Packit 0b5880
  srunner_run_all (sr, CK_VERBOSE);
Packit 0b5880
Packit 0b5880
  ck_assert_msg (!test_tc11_executed
Packit 0b5880
               && test_tc12_executed
Packit 0b5880
               && !test_tc21_executed,
Packit 0b5880
               "Expected tests were not executed.");
Packit 0b5880
  
Packit 0b5880
  reset_executed ();
Packit 0b5880
  unsetenv ("CK_RUN_CASE");
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_srunner_no_tcase_env)
Packit 0b5880
{
Packit 0b5880
  /* This test makes the srunner_run_all function to run a
Packit 0b5880
     non-existant test case.  */
Packit 0b5880
  setenv ("CK_RUN_CASE", "non-existant-test-case", 1);
Packit 0b5880
  srunner_run_all (sr, CK_VERBOSE);
Packit 0b5880
Packit 0b5880
  ck_assert_msg (!(test_tc11_executed
Packit 0b5880
           || test_tc12_executed
Packit 0b5880
           || test_tc21_executed),
Packit 0b5880
           "An unexpected test was executed.");
Packit 0b5880
Packit 0b5880
  reset_executed ();
Packit 0b5880
  unsetenv ("CK_RUN_CASE");
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_srunner_suite_tcase_env)
Packit 0b5880
{
Packit 0b5880
  /* This test makes the srunner_run_all function to run a specific test
Packit 0b5880
     case of a specific test suite.  */
Packit 0b5880
  setenv ("CK_RUN_SUITE", "suite2", 1);
Packit 0b5880
  setenv ("CK_RUN_CASE", "tcase21", 1);
Packit 0b5880
  srunner_run_all (sr, CK_VERBOSE);
Packit 0b5880
  
Packit 0b5880
  ck_assert_msg (!test_tc11_executed
Packit 0b5880
               && !test_tc12_executed
Packit 0b5880
               && test_tc21_executed,
Packit 0b5880
               "Expected tests were not executed.");
Packit 0b5880
Packit 0b5880
  reset_executed ();
Packit 0b5880
  unsetenv ("CK_RUN_SUITE");
Packit 0b5880
  unsetenv ("CK_RUN_CASE");
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
Packit 0b5880
START_TEST(test_srunner_suite_no_tcase_env)
Packit 0b5880
{
Packit 0b5880
  /* This test makes the srunner_run_all function to run a non
Packit 0b5880
     existant test case of a specific test suite.  */
Packit 0b5880
  setenv ("CK_RUN_SUITE", "suite1", 1);
Packit 0b5880
  setenv ("CK_RUN_CASE", "non-existant-test-case", 1);
Packit 0b5880
  srunner_run_all (sr, CK_VERBOSE);
Packit 0b5880
Packit 0b5880
  ck_assert_msg (!(test_tc11_executed
Packit 0b5880
           || test_tc12_executed
Packit 0b5880
           || test_tc21_executed),
Packit 0b5880
           "An unexpected test was executed.");
Packit 0b5880
Packit 0b5880
  reset_executed ();
Packit 0b5880
  unsetenv ("CK_RUN_SUITE");
Packit 0b5880
  unsetenv ("CK_RUN_CASE");
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
#endif /* HAVE_DECL_SETENV */
Packit 0b5880
Packit 0b5880
Suite *make_selective_suite (void)
Packit 0b5880
{
Packit 0b5880
  Suite *s = suite_create ("SelectiveTesting");
Packit 0b5880
  TCase *tc = tcase_create ("Core");
Packit 0b5880
Packit 0b5880
  suite_add_tcase (s, tc);
Packit 0b5880
  tcase_add_test (tc, test_srunner_run_run_all);
Packit 0b5880
  tcase_add_test (tc, test_srunner_run_suite);
Packit 0b5880
  tcase_add_test (tc, test_srunner_run_no_suite);
Packit 0b5880
  tcase_add_test (tc, test_srunner_run_tcase);
Packit 0b5880
  tcase_add_test (tc, test_srunner_no_tcase);
Packit 0b5880
  tcase_add_test (tc, test_srunner_suite_tcase);
Packit 0b5880
  tcase_add_test (tc, test_srunner_suite_no_tcase);
Packit 0b5880
Packit 0b5880
#if HAVE_DECL_SETENV
Packit 0b5880
  tcase_add_test (tc, test_srunner_run_suite_env);
Packit 0b5880
  tcase_add_test (tc, test_srunner_run_no_suite_env);
Packit 0b5880
  tcase_add_test (tc, test_srunner_run_tcase_env);
Packit 0b5880
  tcase_add_test (tc, test_srunner_no_tcase_env);
Packit 0b5880
  tcase_add_test (tc, test_srunner_suite_tcase_env);
Packit 0b5880
  tcase_add_test (tc, test_srunner_suite_no_tcase_env);
Packit 0b5880
#endif /* HAVE_DECL_SETENV */
Packit 0b5880
Packit 0b5880
  tcase_add_unchecked_fixture (tc,
Packit 0b5880
                               selective_setup,
Packit 0b5880
                               selective_teardown);
Packit 0b5880
  return s;
Packit 0b5880
}