~ubuntu-branches/debian/sid/glib2.0/sid

« back to all changes in this revision

Viewing changes to glib/gtestutils.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2013-05-08 06:25:57 UTC
  • mfrom: (1.27.14) (3.1.181 experimental)
  • Revision ID: package-import@ubuntu.com-20130508062557-i7gbku66mls70gi2
Tags: 2.36.1-2
Merge experimental branch, upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#include <sys/wait.h>
29
29
#include <sys/time.h>
30
30
#include <fcntl.h>
 
31
#include <glib/gstdio.h>
31
32
#endif
32
33
#include <string.h>
33
34
#include <stdlib.h>
105
106
 */
106
107
 
107
108
/**
 
109
 * g_test_initialized:
 
110
 *
 
111
 * Returns %TRUE if g_test_init() has been called.
 
112
 *
 
113
 * Returns: %TRUE if g_test_init() has been called.
 
114
 *
 
115
 * Since: 2.36
 
116
 */
 
117
 
 
118
/**
108
119
 * g_test_quick:
109
120
 *
110
121
 * Returns %TRUE if tests are run in quick mode.
573
584
        {
574
585
          g_printerr (":(");
575
586
          for (ui = 0; ui < msg->n_nums; ui++)
576
 
            g_printerr ("%s%.16Lg", ui ? ";" : "", msg->nums[ui]);
 
587
            {
 
588
              if ((long double) (long) msg->nums[ui] == msg->nums[ui])
 
589
                g_printerr ("%s%ld", ui ? ";" : "", (long) msg->nums[ui]);
 
590
              else
 
591
                g_printerr ("%s%.16g", ui ? ";" : "", (double) msg->nums[ui]);
 
592
            }
577
593
          g_printerr (")");
578
594
        }
579
595
      g_printerr (":LOG*}\n");
617
633
        g_print ("(MAXPERF:%s)\n", string1);
618
634
      break;
619
635
    case G_TEST_LOG_MESSAGE:
 
636
    case G_TEST_LOG_ERROR:
620
637
      if (g_test_verbose())
621
638
        g_print ("(MSG: %s)\n", string1);
622
639
      break;
784
801
            }
785
802
          argv[i] = NULL;
786
803
        }
787
 
      else if (strcmp ("-?", argv[i]) == 0 || strcmp ("--help", argv[i]) == 0)
 
804
      else if (strcmp ("-?", argv[i]) == 0 ||
 
805
               strcmp ("-h", argv[i]) == 0 ||
 
806
               strcmp ("--help", argv[i]) == 0)
788
807
        {
789
808
          printf ("Usage:\n"
790
809
                  "  %s [OPTION...]\n\n"
791
810
                  "Help Options:\n"
792
 
                  "  -?, --help                     Show help options\n"
 
811
                  "  -h, --help                     Show help options\n\n"
793
812
                  "Test Options:\n"
 
813
                  "  --g-fatal-warnings             Make all warnings fatal\n"
794
814
                  "  -l                             List test cases available in a test executable\n"
795
 
                  "  -seed=RANDOMSEED               Provide a random seed to reproduce test\n"
796
 
                  "                                 runs using random numbers\n"
797
 
                  "  --verbose                      Run tests verbosely\n"
 
815
                  "  -m {perf|slow|thorough|quick}  Execute tests according to mode\n"
 
816
                  "  -m {undefined|no-undefined}    Execute tests according to mode\n"
 
817
                  "  -p TESTPATH                    Only start test cases matching TESTPATH\n"
 
818
                  "  -s TESTPATH                    Skip all tests matching TESTPATH\n"
 
819
                  "  -seed=SEEDSTRING               Start tests with random seed SEEDSTRING\n"
 
820
                  "  --debug-log                    debug test logging output\n"
798
821
                  "  -q, --quiet                    Run tests quietly\n"
799
 
                  "  -p TESTPATH                    execute all tests matching TESTPATH\n"
800
 
                  "  -s TESTPATH                    skip all tests matching TESTPATH\n"
801
 
                  "  -m {perf|slow|thorough|quick}  Execute tests according modes\n"
802
 
                  "  -m {undefined|no-undefined}    Execute tests according modes\n"
803
 
                  "  --debug-log                    debug test logging output\n"
804
 
                  "  -k, --keep-going               gtester-specific argument\n"
805
 
                  "  --GTestLogFD=N                 gtester-specific argument\n"
806
 
                  "  --GTestSkipCount=N             gtester-specific argument\n",
 
822
                  "  --verbose                      Run tests verbosely\n",
807
823
                  argv[0]);
808
824
          exit (0);
809
825
        }
836
852
 *   <varlistentry>
837
853
 *     <term><option>-l</option></term>
838
854
 *     <listitem><para>
839
 
 *       list test cases available in a test executable.
 
855
 *       List test cases available in a test executable.
840
856
 *     </para></listitem>
841
857
 *   </varlistentry>
842
858
 *   <varlistentry>
843
859
 *     <term><option>--seed=<replaceable>RANDOMSEED</replaceable></option></term>
844
860
 *     <listitem><para>
845
 
 *       provide a random seed to reproduce test runs using random numbers.
 
861
 *       Provide a random seed to reproduce test runs using random numbers.
846
862
 *     </para></listitem>
847
863
 *     </varlistentry>
848
864
 *     <varlistentry>
849
865
 *       <term><option>--verbose</option></term>
850
 
 *       <listitem><para>run tests verbosely.</para></listitem>
 
866
 *       <listitem><para>Run tests verbosely.</para></listitem>
851
867
 *     </varlistentry>
852
868
 *     <varlistentry>
853
869
 *       <term><option>-q</option>, <option>--quiet</option></term>
854
 
 *       <listitem><para>run tests quietly.</para></listitem>
 
870
 *       <listitem><para>Run tests quietly.</para></listitem>
855
871
 *     </varlistentry>
856
872
 *     <varlistentry>
857
873
 *       <term><option>-p <replaceable>TESTPATH</replaceable></option></term>
858
874
 *       <listitem><para>
859
 
 *         execute all tests matching <replaceable>TESTPATH</replaceable>.
 
875
 *         Execute all tests matching <replaceable>TESTPATH</replaceable>.
860
876
 *       </para></listitem>
861
877
 *     </varlistentry>
862
878
 *     <varlistentry>
863
879
 *       <term><option>-m {perf|slow|thorough|quick|undefined|no-undefined}</option></term>
864
880
 *       <listitem><para>
865
 
 *         execute tests according to these test modes:
 
881
 *         Execute tests according to these test modes:
866
882
 *         <variablelist>
867
883
 *           <varlistentry>
868
884
 *             <term>perf</term>
869
885
 *             <listitem><para>
870
 
 *               performance tests, may take long and report results.
 
886
 *               Performance tests, may take long and report results.
871
887
 *             </para></listitem>
872
888
 *           </varlistentry>
873
889
 *           <varlistentry>
874
890
 *             <term>slow, thorough</term>
875
891
 *             <listitem><para>
876
 
 *               slow and thorough tests, may take quite long and 
 
892
 *               Slow and thorough tests, may take quite long and
877
893
 *               maximize coverage.
878
894
 *             </para></listitem>
879
895
 *           </varlistentry>
880
896
 *           <varlistentry>
881
897
 *             <term>quick</term>
882
898
 *             <listitem><para>
883
 
 *               quick tests, should run really quickly and give good coverage.
 
899
 *               Quick tests, should run really quickly and give good coverage.
884
900
 *             </para></listitem>
885
901
 *           </varlistentry>
886
902
 *           <varlistentry>
887
903
 *             <term>undefined</term>
888
904
 *             <listitem><para>
889
 
 *               tests for undefined behaviour, may provoke programming errors
 
905
 *               Tests for undefined behaviour, may provoke programming errors
890
906
 *               under g_test_trap_fork() to check that appropriate assertions
891
907
 *               or warnings are given
892
908
 *             </para></listitem>
894
910
 *           <varlistentry>
895
911
 *             <term>no-undefined</term>
896
912
 *             <listitem><para>
897
 
 *               avoid tests for undefined behaviour
 
913
 *               Avoid tests for undefined behaviour
898
914
 *             </para></listitem>
899
915
 *           </varlistentry>
900
916
 *         </variablelist>
902
918
 *     </varlistentry>
903
919
 *     <varlistentry>
904
920
 *       <term><option>--debug-log</option></term>
905
 
 *       <listitem><para>debug test logging output.</para></listitem>
906
 
 *     </varlistentry>
907
 
 *     <varlistentry>
908
 
 *       <term><option>-k</option>, <option>--keep-going</option></term>
909
 
 *       <listitem><para>gtester-specific argument.</para></listitem>
910
 
 *     </varlistentry>
911
 
 *     <varlistentry>
912
 
 *       <term><option>--GTestLogFD <replaceable>N</replaceable></option></term>
913
 
 *       <listitem><para>gtester-specific argument.</para></listitem>
914
 
 *     </varlistentry>
915
 
 *     <varlistentry>
916
 
 *       <term><option>--GTestSkipCount <replaceable>N</replaceable></option></term>
917
 
 *       <listitem><para>gtester-specific argument.</para></listitem>
 
921
 *       <listitem><para>Debug test logging output.</para></listitem>
918
922
 *     </varlistentry>
919
923
 *  </variablelist>
920
924
 *
1374
1378
  return tc;
1375
1379
}
1376
1380
 
 
1381
static gint
 
1382
find_suite (gconstpointer l, gconstpointer s)
 
1383
{
 
1384
  const GTestSuite *suite = l;
 
1385
  const gchar *str = s;
 
1386
 
 
1387
  return strcmp (suite->name, str);
 
1388
}
 
1389
 
1377
1390
/**
1378
1391
 * GTestFixtureFunc:
1379
1392
 * @fixture: the test fixture
1423
1436
        continue;       /* initial or duplicate slash */
1424
1437
      else if (!islast)
1425
1438
        {
1426
 
          GTestSuite *csuite = g_test_create_suite (seg);
1427
 
          g_test_suite_add_suite (suite, csuite);
 
1439
          GSList *l;
 
1440
          GTestSuite *csuite;
 
1441
          l = g_slist_find_custom (suite->suites, seg, find_suite);
 
1442
          if (l)
 
1443
            {
 
1444
              csuite = l->data;
 
1445
            }
 
1446
          else
 
1447
            {
 
1448
              csuite = g_test_create_suite (seg);
 
1449
              g_test_suite_add_suite (suite, csuite);
 
1450
            }
1428
1451
          suite = csuite;
1429
1452
        }
1430
1453
      else /* islast */
1523
1546
  g_return_if_fail (testpath != NULL);
1524
1547
  g_return_if_fail (testpath[0] == '/');
1525
1548
  g_return_if_fail (test_func != NULL);
 
1549
 
1526
1550
  g_test_add_vtable (testpath, 0, test_data, NULL, (GTestFixtureFunc) test_func, NULL);
1527
1551
}
1528
1552
 
1529
1553
/**
 
1554
 * g_test_add_data_func_full:
 
1555
 * @testpath: /-separated test case path name for the test.
 
1556
 * @test_data: Test data argument for the test function.
 
1557
 * @test_func: The test function to invoke for this test.
 
1558
 * @data_free_func: #GDestroyNotify for @test_data.
 
1559
 *
 
1560
 * Create a new test case, as with g_test_add_data_func(), but freeing
 
1561
 * @test_data after the test run is complete.
 
1562
 *
 
1563
 * Since: 2.34
 
1564
 */
 
1565
void
 
1566
g_test_add_data_func_full (const char     *testpath,
 
1567
                           gpointer        test_data,
 
1568
                           GTestDataFunc   test_func,
 
1569
                           GDestroyNotify  data_free_func)
 
1570
{
 
1571
  g_return_if_fail (testpath != NULL);
 
1572
  g_return_if_fail (testpath[0] == '/');
 
1573
  g_return_if_fail (test_func != NULL);
 
1574
 
 
1575
  g_test_add_vtable (testpath, 0, test_data, NULL,
 
1576
                     (GTestFixtureFunc) test_func,
 
1577
                     (GTestFixtureFunc) data_free_func);
 
1578
}
 
1579
 
 
1580
/**
1530
1581
 * g_test_create_suite:
1531
1582
 * @suite_name: a name for the suite
1532
1583
 *
1885
1936
                            char            numtype)
1886
1937
{
1887
1938
  char *s = NULL;
 
1939
 
1888
1940
  switch (numtype)
1889
1941
    {
1890
 
    case 'i':   s = g_strdup_printf ("assertion failed (%s): (%.0Lf %s %.0Lf)", expr, arg1, cmp, arg2); break;
 
1942
    case 'i':   s = g_strdup_printf ("assertion failed (%s): (%" G_GINT64_MODIFIER "i %s %" G_GINT64_MODIFIER "i)", expr, (gint64) arg1, cmp, (gint64) arg2); break;
1891
1943
    case 'x':   s = g_strdup_printf ("assertion failed (%s): (0x%08" G_GINT64_MODIFIER "x %s 0x%08" G_GINT64_MODIFIER "x)", expr, (guint64) arg1, cmp, (guint64) arg2); break;
1892
 
    case 'f':   s = g_strdup_printf ("assertion failed (%s): (%.9Lg %s %.9Lg)", expr, arg1, cmp, arg2); break;
 
1944
    case 'f':   s = g_strdup_printf ("assertion failed (%s): (%.9g %s %.9g)", expr, (double) arg1, cmp, (double) arg2); break;
1893
1945
      /* ideally use: floats=%.7g double=%.17g */
1894
1946
    }
1895
1947
  g_assertion_message (domain, file, line, func, s);
1957
2009
 * @str1: (allow-none): a C string or %NULL
1958
2010
 * @str2: (allow-none): another C string or %NULL
1959
2011
 *
1960
 
 * Compares @str1 and @str2 like strcmp(). Handles %NULL 
 
2012
 * Compares @str1 and @str2 like strcmp(). Handles %NULL
1961
2013
 * gracefully by sorting it before non-%NULL strings.
1962
2014
 * Comparing two %NULL pointers returns 0.
1963
2015
 *
1964
 
 * Returns: -1, 0 or 1, if @str1 is <, == or > than @str2.
 
2016
 * Returns: an integer less than, equal to, or greater than zero, if @str1 is <, == or > than @str2.
1965
2017
 *
1966
2018
 * Since: 2.16
1967
2019
 */
2162
2214
      close (stderr_pipe[0]);
2163
2215
      close (stdtst_pipe[0]);
2164
2216
      if (!(test_trap_flags & G_TEST_TRAP_INHERIT_STDIN))
2165
 
        fd0 = open ("/dev/null", O_RDONLY);
 
2217
        fd0 = g_open ("/dev/null", O_RDONLY, 0);
2166
2218
      if (sane_dup2 (stdout_pipe[1], 1) < 0 || sane_dup2 (stderr_pipe[1], 2) < 0 || (fd0 >= 0 && sane_dup2 (fd0, 0) < 0))
2167
2219
        g_error ("failed to dup2() in forked test program: %s", g_strerror (errno));
2168
2220
      if (fd0 >= 3)