"Fossies" - the Fresh Open Source Software Archive

Member "check-0.13.0/tests/check_thread_stress.c" (22 Oct 2019, 2485 Bytes) of package /linux/privat/check-0.13.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "check_thread_stress.c": 0.12.0_vs_0.13.0.

    1 /*
    2  * Check: a unit test framework for C
    3  * Copyright (C) 2001, 2002 Arien Malec
    4  *
    5  * This library is free software; you can redistribute it and/or
    6  * modify it under the terms of the GNU Lesser General Public
    7  * License as published by the Free Software Foundation; either
    8  * version 2.1 of the License, or (at your option) any later version.
    9  *
   10  * This library is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   13  * Lesser General Public License for more details.
   14  *
   15  * You should have received a copy of the GNU Lesser General Public
   16  * License along with this library; if not, write to the
   17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
   18  * MA 02110-1301, USA.
   19  */
   20 
   21 #include "../lib/libcompat.h"
   22 
   23 #include <stdlib.h>
   24 #include <stdio.h>
   25 #include <check.h>
   26 
   27 Suite *s;
   28 TCase *tc;
   29 SRunner *sr;
   30 
   31 #if defined (HAVE_PTHREAD) || defined (HAVE_FORK)
   32 static void *
   33 sendinfo (void *userdata CK_ATTRIBUTE_UNUSED)
   34 {
   35   unsigned int i;
   36   for (i = 0; i < 999; i++)
   37     {
   38       ck_assert_msg (1, "Shouldn't see this message");
   39     }
   40   return NULL;
   41 }
   42 #endif /* HAVE_PTHREAD || HAVE_FORK */
   43 
   44 #ifdef HAVE_PTHREAD
   45 START_TEST (test_stress_threads)
   46 {
   47   pthread_t a, b;
   48   pthread_create (&a, NULL, sendinfo, (void *) 0xa);
   49   pthread_create (&b, NULL, sendinfo, (void *) 0xb);
   50 
   51   pthread_join (a, NULL);
   52   pthread_join (b, NULL);
   53 }
   54 END_TEST
   55 #endif /* HAVE_PTHREAD */
   56 
   57 #if defined(HAVE_FORK) && HAVE_FORK==1
   58 START_TEST (test_stress_forks)
   59 {
   60   pid_t cpid = fork ();
   61   if (cpid == 0)
   62     {
   63       /* child */
   64       sendinfo ((void *) 0x1);
   65       exit (EXIT_SUCCESS);
   66     }
   67   else
   68     {
   69       /* parent */
   70       sendinfo ((void *) 0x2);
   71     }
   72 }
   73 END_TEST
   74 #endif /* HAVE_FORK */
   75 
   76 int
   77 main (void)
   78 {
   79   int number_failed;
   80   s = suite_create ("ForkThreadStress");
   81   tc = tcase_create ("ForkThreadStress");
   82   sr = srunner_create (s);
   83   suite_add_tcase (s, tc);
   84 
   85 #ifdef HAVE_PTHREAD
   86   tcase_add_loop_test (tc, test_stress_threads, 0, 100);
   87 #endif /* HAVE_PTHREAD */
   88 
   89 #if defined(HAVE_FORK) && HAVE_FORK==1
   90   tcase_add_loop_test (tc, test_stress_forks, 0, 100);
   91 #endif /* HAVE_FORK */
   92 
   93   srunner_run_all (sr, CK_VERBOSE);
   94   number_failed = srunner_ntests_failed (sr);
   95   srunner_free (sr);
   96 
   97   /* hack to give us XFAIL on non-posix platforms */
   98 #ifndef HAVE_FORK
   99   number_failed++;
  100 #endif /* !HAVE_FORK */
  101 
  102   return number_failed ? EXIT_FAILURE : EXIT_SUCCESS;
  103 }