From aa968dde60d1c3cff4dcebcca4b6f5c2b9737829 Mon Sep 17 00:00:00 2001 From: Yannick Brosseau Date: Fri, 16 Nov 2012 10:27:21 -0500 Subject: [PATCH] Include a test for SEEK_BEGIN and SEEK_LAST [ Edit by Mathieu Desnoyers: style fixes, add NR_TESTS define. ] Signed-off-by: Yannick Brosseau Signed-off-by: Mathieu Desnoyers --- .gitignore | 1 + configure.ac | 1 + tests/Makefile.am | 2 + .../succeed/wk-heartbeat-u/metadata | Bin 0 -> 4096 bytes tests/ctf-traces/succeed/wk-heartbeat-u/u_0 | Bin 0 -> 4096 bytes tests/ctf-traces/succeed/wk-heartbeat-u/u_1 | Bin 0 -> 4096 bytes tests/ctf-traces/succeed/wk-heartbeat-u/u_2 | Bin 0 -> 4096 bytes tests/ctf-traces/succeed/wk-heartbeat-u/u_3 | Bin 0 -> 4096 bytes tests/ctf-traces/succeed/wk-heartbeat-u/u_4 | Bin 0 -> 4096 bytes tests/ctf-traces/succeed/wk-heartbeat-u/u_5 | Bin 0 -> 4096 bytes tests/ctf-traces/succeed/wk-heartbeat-u/u_6 | Bin 0 -> 4096 bytes tests/ctf-traces/succeed/wk-heartbeat-u/u_7 | Bin 0 -> 4096 bytes tests/lib/Makefile.am | 16 + tests/lib/README.tap | 2 + tests/lib/common.c | 42 ++ tests/lib/common.h | 28 ++ tests/lib/runall.sh | 3 + tests/lib/tap.c | 426 ++++++++++++++++++ tests/lib/tap.h | 89 ++++ tests/lib/test-seeks.c | 167 +++++++ 20 files changed, 777 insertions(+) create mode 100755 tests/ctf-traces/succeed/wk-heartbeat-u/metadata create mode 100755 tests/ctf-traces/succeed/wk-heartbeat-u/u_0 create mode 100755 tests/ctf-traces/succeed/wk-heartbeat-u/u_1 create mode 100755 tests/ctf-traces/succeed/wk-heartbeat-u/u_2 create mode 100755 tests/ctf-traces/succeed/wk-heartbeat-u/u_3 create mode 100755 tests/ctf-traces/succeed/wk-heartbeat-u/u_4 create mode 100755 tests/ctf-traces/succeed/wk-heartbeat-u/u_5 create mode 100755 tests/ctf-traces/succeed/wk-heartbeat-u/u_6 create mode 100755 tests/ctf-traces/succeed/wk-heartbeat-u/u_7 create mode 100644 tests/lib/Makefile.am create mode 100644 tests/lib/README.tap create mode 100644 tests/lib/common.c create mode 100644 tests/lib/common.h create mode 100755 tests/lib/runall.sh create mode 100644 tests/lib/tap.c create mode 100644 tests/lib/tap.h create mode 100644 tests/lib/test-seeks.c diff --git a/.gitignore b/.gitignore index d6098acd..8c6a20b6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /tests/test-bitfield +/tests/lib/test-seeks *.o *.a *.la diff --git a/configure.ac b/configure.ac index d9de0159..19f16a63 100644 --- a/configure.ac +++ b/configure.ac @@ -106,6 +106,7 @@ AC_CONFIG_FILES([ lib/prio_heap/Makefile include/Makefile tests/Makefile + tests/lib/Makefile extras/Makefile extras/valgrind/Makefile ]) diff --git a/tests/Makefile.am b/tests/Makefile.am index ff68240e..47891e90 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,5 +1,7 @@ AM_CFLAGS = $(PACKAGE_CFLAGS) -I$(top_srcdir)/include +SUBDIRS = lib + noinst_PROGRAMS = test-bitfield test_bitfield_SOURCES = test-bitfield.c diff --git a/tests/ctf-traces/succeed/wk-heartbeat-u/metadata b/tests/ctf-traces/succeed/wk-heartbeat-u/metadata new file mode 100755 index 0000000000000000000000000000000000000000..e35f463e09d56d3bae8349163ef7439d585e04b5 GIT binary patch literal 4096 zcmeHJ&2G~`5QZDc8}xv*>Zz^j)c>^`iVz5dgh1lN0aey|y>Yj-*T!B43aW&}`)~%{ zfdfa5ya_XFJN--3AR!^4L{YRE&wMlU?fm%F-5*W-Z0+aT_ZOePefjk8*Vm(iTkv`C z5`I47>DKK#>ZIf}6SO8mDxUI+d?2-W&&fUs#sr?EvOU&t;t8B5G^_cToRYnL(tuGA zsqt_DjO8zfWBE4#Irefuj(r1=?=A=AyEg!NXE@dvXuF0X?*gQTSFn5)qtrznJru2t1Ag-!=}=SVThX(|{fYY9{VA?WzEH6~kj$!HzM&4n8krp$e&!?YU)E7<-h3 zMjUY8a3{Ng5%bV9sBe0S>2k+qzK)#NtP-UXg%1CD4@DlK6V#Yg{0;cF%>FYbJ6q%l zNEW@v|@>jk#!*iM`EoE9Dqx>2yWLa`D{NI}Oonz1#~Q37~BDWHC}N;Fex>-p*k znmJzF^UFGhI{Z_|i)X~Lj$wk#bKG@P+hq|eVDi4x&4s-+&xR@EnPAiJ!&Tgq?CiDPKnDV zCwqERz#N3_(m&fLCK-)J#4)!Cqz}RB_2O8tTJ*4((n?UN$UTwV$8H>)8$vp(dG&P0 z6r?dY?Y-fj%jSAIX;1A4TTFZl*9Qv!PRSH&Td&BCVDlO#GFqj2SfAqoH{Bc}e9v>- z3tXVYTbhn}RsN6ZfWeP9r^h@FU0ozs*UG%w0qt%J_5E?ShAuYF4EL%c_#Rw&ueolf zzzTlJFwA5j;VmboH7+E}#221us@ih-n~<;rA%T7|Vv8bU{bHMK!M(graa+u& zglcZrDujpBW<}bsF$*U8U?O;y)R%TQ7-4o1fPqTqRKE$XepU>u7+5i|V&K1F;5Qut BI)?xN literal 0 HcmV?d00001 diff --git a/tests/ctf-traces/succeed/wk-heartbeat-u/u_0 b/tests/ctf-traces/succeed/wk-heartbeat-u/u_0 new file mode 100755 index 0000000000000000000000000000000000000000..3af2cdc159e368c17d1918418c028f616e9eec7d GIT binary patch literal 4096 zcmX>o|L0(mx8zO9vwrJ#?Pzv-xHBu30SNY7eJOv6k%8gCqUQ=A8Uzl2=mrpL6pV(z oXb6mkz-S1JhQMeDjE2By2#kinXb6mkz-S1JhQMeD45bhN0I7cxumAu6 literal 0 HcmV?d00001 diff --git a/tests/ctf-traces/succeed/wk-heartbeat-u/u_1 b/tests/ctf-traces/succeed/wk-heartbeat-u/u_1 new file mode 100755 index 0000000000000000000000000000000000000000..7b74c0352c13596f4e5fd87f2d11f87c1fa2ae67 GIT binary patch literal 4096 zcmX>o|L0(mx8zO9vwrJ#?Pzv-xHBu30SK6Xyplh~$iVPXOji*^gTMh0-N3*A<${Dp r!DtAKhQMeDjE2By2#kinXb6mkz-S1JhQMeDjE2By2#kin5C{PPUH1^^ literal 0 HcmV?d00001 diff --git a/tests/ctf-traces/succeed/wk-heartbeat-u/u_2 b/tests/ctf-traces/succeed/wk-heartbeat-u/u_2 new file mode 100755 index 0000000000000000000000000000000000000000..13a69fc124a1ea9b82f5a301648591a99928ab88 GIT binary patch literal 4096 zcmX>o|L0(mx8zO9vwrJ#?Pzv-xHBu30SJ8U-pHR~WMB|>(NqM{An<|%NHj1oFaarf zAZGaTvP}Ub*~i1c&;i65sfk4;NvVk?3<(e431W&i7=9AM6n&uhQUp^}!Eml9rf5R0 zofxL*gZEFdh%T^yERLzRVM(AQrs#uZ-?4~-{5T3mLtr!nMnhmU1V%$(Gz3ONU^E0q TLtr!nMnhmU1V%$(7=!=-nYdK# literal 0 HcmV?d00001 diff --git a/tests/ctf-traces/succeed/wk-heartbeat-u/u_3 b/tests/ctf-traces/succeed/wk-heartbeat-u/u_3 new file mode 100755 index 0000000000000000000000000000000000000000..1cdde72773d8b36247ec78b665d6f8d57f347e3b GIT binary patch literal 4096 zcmX>o|L0(mx8zO9vwrJ#?Pzv-xHBu30SKgo-^!n2WMDWgs-Xy?LEr$0ZeU}2mAP@5Z literal 0 HcmV?d00001 diff --git a/tests/ctf-traces/succeed/wk-heartbeat-u/u_4 b/tests/ctf-traces/succeed/wk-heartbeat-u/u_4 new file mode 100755 index 0000000000000000000000000000000000000000..c1360344a938335e16acac3a83d800a8340aea47 GIT binary patch literal 4096 zcmX>o|L0(mx8zO9vwrJ#?Pzv-xHBu30SLT(-pQY0WMJ46VX6qCLBN6oNHj1oumCA} zAZEBP+@S!H?B`)%=m6r3)Wo8aq}0R`h67fI1u;bve)R}piaLA=7seD-*u*1(MYQKP z7SRQJmx*GkWjOpp42x*Q2XQQ-An%NV(GVC7fzc2c4S~@R7!85Z5Eu=C(GVC7fzc2c M4S~@R7_K1z0Ia}E(*OVf literal 0 HcmV?d00001 diff --git a/tests/ctf-traces/succeed/wk-heartbeat-u/u_5 b/tests/ctf-traces/succeed/wk-heartbeat-u/u_5 new file mode 100755 index 0000000000000000000000000000000000000000..a0a588176b801577692a3379137920364446ec04 GIT binary patch literal 4096 zcmX>o|L0(mx8zO9vwrJ#?Pzv-xHBu30SH{U-^-t3WMHW5Q&$AhAaDRgH!v`;f;gjK qGz3ONU^E0qLtr!nMnhmU1V%$(Gz3ONU^E0qLtr!nMnho8ga82DVGut6 literal 0 HcmV?d00001 diff --git a/tests/ctf-traces/succeed/wk-heartbeat-u/u_6 b/tests/ctf-traces/succeed/wk-heartbeat-u/u_6 new file mode 100755 index 0000000000000000000000000000000000000000..b1c8520e533bc8ddea04941778bc0dff77dc7e37 GIT binary patch literal 4096 zcmX>o|L0(mx8zO9vwrJ#?Pzv-xHBu30SGSJe2_oI$iOh|SBfHt27w(+K%#+xfelE> z12Kcg$Kwhh$$lONh7KUkNKGs%NlHyD0ZER6(GVC7fzc2c4S~@R7!85Z5Eu=C(GVC7 Sfzc2c4S~@R7!85J5&{4ncp0ex literal 0 HcmV?d00001 diff --git a/tests/ctf-traces/succeed/wk-heartbeat-u/u_7 b/tests/ctf-traces/succeed/wk-heartbeat-u/u_7 new file mode 100755 index 0000000000000000000000000000000000000000..05f7393e8d32159ac81a669512c31b1ac58f1a53 GIT binary patch literal 4096 zcmX>o|L0(mx8zO9vwrJ#?Pzv-xHBu30SJEm{UCpek%1wvCsPqbgTMh0-N3-W4&sc0 q(GVC7fzc2c4S~@R7!85Z5Eu=C(GVC7fzc2c4S~@R7!83T69NFP%@Q8~ literal 0 HcmV?d00001 diff --git a/tests/lib/Makefile.am b/tests/lib/Makefile.am new file mode 100644 index 00000000..e846680a --- /dev/null +++ b/tests/lib/Makefile.am @@ -0,0 +1,16 @@ +AM_CFLAGS = $(PACKAGE_CFLAGS) -I$(top_srcdir)/include + +noinst_LIBRARIES = libtestcommon.a + +libtestcommon_a_SOURCES = tap.c common.c + +test_seeks_LDADD = libtestcommon.a \ + $(top_builddir)/lib/libbabeltrace.la \ + $(top_builddir)/formats/ctf/libbabeltrace-ctf.la + +noinst_PROGRAMS = test-seeks + +test_seeks_SOURCES = test-seeks.c + +check-am: + ./runall.sh diff --git a/tests/lib/README.tap b/tests/lib/README.tap new file mode 100644 index 00000000..e99e7b2f --- /dev/null +++ b/tests/lib/README.tap @@ -0,0 +1,2 @@ +tap.c and tap.h were taken from +http://jc.ngo.org.uk/trac-bin/trac.cgi/wiki/LibTap diff --git a/tests/lib/common.c b/tests/lib/common.c new file mode 100644 index 00000000..25bf735e --- /dev/null +++ b/tests/lib/common.c @@ -0,0 +1,42 @@ +/* + * common.c + * + * Lib BabelTrace - Common function to all tests + * + * Copyright 2012 - Yannick Brosseau + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; under version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#define _GNU_SOURCE + +#include +#include + +struct bt_context *create_context_with_path(const char *path) +{ + struct bt_context *ctx; + int ret; + + ctx = bt_context_create(); + if (!ctx) { + return NULL; + } + + ret = bt_context_add_trace(ctx, path, "ctf", NULL, NULL, NULL); + if (ret < 0) { + bt_context_put(ctx); + return NULL; + } + return ctx; +} diff --git a/tests/lib/common.h b/tests/lib/common.h new file mode 100644 index 00000000..25a02ffa --- /dev/null +++ b/tests/lib/common.h @@ -0,0 +1,28 @@ +/* + * common.h + * + * Lib BabelTrace - Common function to all tests + * + * Copyright 2012 - Yannick Brosseau + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; under version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#ifndef _TESTS_COMMON_H +#define _TESTS_COMMON_H + +struct bt_context; + +struct bt_context *create_context_with_path(const char *path); + +#endif /* _TESTS_COMMON_H */ diff --git a/tests/lib/runall.sh b/tests/lib/runall.sh new file mode 100755 index 00000000..2f1b5d7a --- /dev/null +++ b/tests/lib/runall.sh @@ -0,0 +1,3 @@ +#!/bin/sh +# run Seeks tests +./test-seeks ../ctf-traces/succeed/wk-heartbeat-u/ 1351532897586558519 1351532897591331194 diff --git a/tests/lib/tap.c b/tests/lib/tap.c new file mode 100644 index 00000000..d41a7518 --- /dev/null +++ b/tests/lib/tap.c @@ -0,0 +1,426 @@ +/*- + * Copyright (c) 2004 Nik Clayton + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include "tap.h" + +static int no_plan = 0; +static int skip_all = 0; +static int have_plan = 0; +static unsigned int test_count = 0; /* Number of tests that have been run */ +static unsigned int e_tests = 0; /* Expected number of tests to run */ +static unsigned int failures = 0; /* Number of tests that failed */ +static char *todo_msg = NULL; +static char *todo_msg_fixed = "libtap malloc issue"; +static int todo = 0; +static int test_died = 0; + +/* Encapsulate the pthread code in a conditional. In the absence of + libpthread the code does nothing */ +#ifdef HAVE_LIBPTHREAD +#include +static pthread_mutex_t M = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&M); +# define UNLOCK pthread_mutex_unlock(&M); +#else +# define LOCK +# define UNLOCK +#endif + +static void _expected_tests(unsigned int); +static void _tap_init(void); +static void _cleanup(void); + +/* + * Generate a test result. + * + * ok -- boolean, indicates whether or not the test passed. + * test_name -- the name of the test, may be NULL + * test_comment -- a comment to print afterwards, may be NULL + */ +unsigned int +_gen_result(int ok, const char *func, char *file, unsigned int line, + char *test_name, ...) +{ + va_list ap; + char *local_test_name = NULL; + char *c; + int name_is_digits; + + LOCK; + + test_count++; + + /* Start by taking the test name and performing any printf() + expansions on it */ + if(test_name != NULL) { + va_start(ap, test_name); + vasprintf(&local_test_name, test_name, ap); + va_end(ap); + + /* Make sure the test name contains more than digits + and spaces. Emit an error message and exit if it + does */ + if(local_test_name) { + name_is_digits = 1; + for(c = local_test_name; *c != '\0'; c++) { + if(!isdigit(*c) && !isspace(*c)) { + name_is_digits = 0; + break; + } + } + + if(name_is_digits) { + diag(" You named your test '%s'. You shouldn't use numbers for your test names.", local_test_name); + diag(" Very confusing."); + } + } + } + + if(!ok) { + printf("not "); + failures++; + } + + printf("ok %d", test_count); + + if(test_name != NULL) { + printf(" - "); + + /* Print the test name, escaping any '#' characters it + might contain */ + if(local_test_name != NULL) { + flockfile(stdout); + for(c = local_test_name; *c != '\0'; c++) { + if(*c == '#') + fputc('\\', stdout); + fputc((int)*c, stdout); + } + funlockfile(stdout); + } else { /* vasprintf() failed, use a fixed message */ + printf("%s", todo_msg_fixed); + } + } + + /* If we're in a todo_start() block then flag the test as being + TODO. todo_msg should contain the message to print at this + point. If it's NULL then asprintf() failed, and we should + use the fixed message. + + This is not counted as a failure, so decrement the counter if + the test failed. */ + if(todo) { + printf(" # TODO %s", todo_msg ? todo_msg : todo_msg_fixed); + if(!ok) + failures--; + } + + printf("\n"); + + if(!ok) { + if(getenv("HARNESS_ACTIVE") != NULL) + fputs("\n", stderr); + + diag(" Failed %stest (%s:%s() at line %d)", + todo ? "(TODO) " : "", file, func, line); + } + free(local_test_name); + + UNLOCK; + + /* We only care (when testing) that ok is positive, but here we + specifically only want to return 1 or 0 */ + return ok ? 1 : 0; +} + +/* + * Initialise the TAP library. Will only do so once, however many times it's + * called. + */ +void +_tap_init(void) +{ + static int run_once = 0; + + if(!run_once) { + atexit(_cleanup); + + /* stdout needs to be unbuffered so that the output appears + in the same place relative to stderr output as it does + with Test::Harness */ + setbuf(stdout, 0); + run_once = 1; + } +} + +/* + * Note that there's no plan. + */ +int +plan_no_plan(void) +{ + + LOCK; + + _tap_init(); + + if(have_plan != 0) { + fprintf(stderr, "You tried to plan twice!\n"); + test_died = 1; + UNLOCK; + exit(255); + } + + have_plan = 1; + no_plan = 1; + + UNLOCK; + + return 1; +} + +/* + * Note that the plan is to skip all tests + */ +int +plan_skip_all(char *reason) +{ + + LOCK; + + _tap_init(); + + skip_all = 1; + + printf("1..0"); + + if(reason != NULL) + printf(" # Skip %s", reason); + + printf("\n"); + + UNLOCK; + + exit(0); +} + +/* + * Note the number of tests that will be run. + */ +int +plan_tests(unsigned int tests) +{ + + LOCK; + + _tap_init(); + + if(have_plan != 0) { + fprintf(stderr, "You tried to plan twice!\n"); + test_died = 1; + UNLOCK; + exit(255); + } + + if(tests == 0) { + fprintf(stderr, "You said to run 0 tests! You've got to run something.\n"); + test_died = 1; + UNLOCK; + exit(255); + } + + have_plan = 1; + + _expected_tests(tests); + + UNLOCK; + + return e_tests; +} + +unsigned int +diag(char *fmt, ...) +{ + va_list ap; + + fputs("# ", stderr); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + fputs("\n", stderr); + + return 0; +} + +void +_expected_tests(unsigned int tests) +{ + + printf("1..%d\n", tests); + e_tests = tests; +} + +int +skip(unsigned int n, char *fmt, ...) +{ + va_list ap; + char *skip_msg; + + LOCK; + + va_start(ap, fmt); + asprintf(&skip_msg, fmt, ap); + va_end(ap); + + while(n-- > 0) { + test_count++; + printf("ok %d # skip %s\n", test_count, + skip_msg != NULL ? + skip_msg : "libtap():malloc() failed"); + } + + free(skip_msg); + + UNLOCK; + + return 1; +} + +void +todo_start(char *fmt, ...) +{ + va_list ap; + + LOCK; + + va_start(ap, fmt); + vasprintf(&todo_msg, fmt, ap); + va_end(ap); + + todo = 1; + + UNLOCK; +} + +void +todo_end(void) +{ + + LOCK; + + todo = 0; + free(todo_msg); + + UNLOCK; +} + +int +exit_status(void) +{ + int r; + + LOCK; + + /* If there's no plan, just return the number of failures */ + if(no_plan || !have_plan) { + UNLOCK; + return failures; + } + + /* Ran too many tests? Return the number of tests that were run + that shouldn't have been */ + if(e_tests < test_count) { + r = test_count - e_tests; + UNLOCK; + return r; + } + + /* Return the number of tests that failed + the number of tests + that weren't run */ + r = failures + e_tests - test_count; + UNLOCK; + + return r; +} + +/* + * Cleanup at the end of the run, produce any final output that might be + * required. + */ +void +_cleanup(void) +{ + + LOCK; + + /* If plan_no_plan() wasn't called, and we don't have a plan, + and we're not skipping everything, then something happened + before we could produce any output */ + if(!no_plan && !have_plan && !skip_all) { + diag("Looks like your test died before it could output anything."); + UNLOCK; + return; + } + + if(test_died) { + diag("Looks like your test died just after %d.", test_count); + UNLOCK; + return; + } + + + /* No plan provided, but now we know how many tests were run, and can + print the header at the end */ + if(!skip_all && (no_plan || !have_plan)) { + printf("1..%d\n", test_count); + } + + if((have_plan && !no_plan) && e_tests < test_count) { + diag("Looks like you planned %d %s but ran %d extra.", + e_tests, e_tests == 1 ? "test" : "tests", test_count - e_tests); + UNLOCK; + return; + } + + if((have_plan || !no_plan) && e_tests > test_count) { + diag("Looks like you planned %d %s but only ran %d.", + e_tests, e_tests == 1 ? "test" : "tests", test_count); + UNLOCK; + return; + } + + if(failures) + diag("Looks like you failed %d %s of %d.", + failures, failures == 1 ? "test" : "tests", test_count); + + UNLOCK; +} diff --git a/tests/lib/tap.h b/tests/lib/tap.h new file mode 100644 index 00000000..0f059430 --- /dev/null +++ b/tests/lib/tap.h @@ -0,0 +1,89 @@ +/*- + * Copyright (c) 2004 Nik Clayton + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* '## __VA_ARGS__' is a gcc'ism. C99 doesn't allow the token pasting + and requires the caller to add the final comma if they've ommitted + the optional arguments */ +#ifdef __GNUC__ +# define ok(e, test, ...) ((e) ? \ + _gen_result(1, __func__, __FILE__, __LINE__, \ + test, ## __VA_ARGS__) : \ + _gen_result(0, __func__, __FILE__, __LINE__, \ + test, ## __VA_ARGS__)) + +# define ok1(e) ((e) ? \ + _gen_result(1, __func__, __FILE__, __LINE__, "%s", #e) : \ + _gen_result(0, __func__, __FILE__, __LINE__, "%s", #e)) + +# define pass(test, ...) ok(1, test, ## __VA_ARGS__); +# define fail(test, ...) ok(0, test, ## __VA_ARGS__); + +# define skip_start(test, n, fmt, ...) \ + do { \ + if((test)) { \ + skip(n, fmt, ## __VA_ARGS__); \ + continue; \ + } +#elif __STDC_VERSION__ >= 199901L /* __GNUC__ */ +# define ok(e, ...) ((e) ? \ + _gen_result(1, __func__, __FILE__, __LINE__, \ + __VA_ARGS__) : \ + _gen_result(0, __func__, __FILE__, __LINE__, \ + __VA_ARGS__)) + +# define ok1(e) ((e) ? \ + _gen_result(1, __func__, __FILE__, __LINE__, "%s", #e) : \ + _gen_result(0, __func__, __FILE__, __LINE__, "%s", #e)) + +# define pass(...) ok(1, __VA_ARGS__); +# define fail(...) ok(0, __VA_ARGS__); + +# define skip_start(test, n, ...) \ + do { \ + if((test)) { \ + skip(n, __VA_ARGS__); \ + continue; \ + } +#else /* __STDC_VERSION__ */ +# error "Needs gcc or C99 compiler for variadic macros." +#endif /* __STDC_VERSION__ */ + +#define skip_end() } while(0); + +unsigned int _gen_result(int, const char *, char *, unsigned int, char *, ...); + +int plan_no_plan(void); +int plan_skip_all(char *); +int plan_tests(unsigned int); + +unsigned int diag(char *, ...); + +int skip(unsigned int, char *, ...); + +void todo_start(char *, ...); +void todo_end(void); + +int exit_status(void); diff --git a/tests/lib/test-seeks.c b/tests/lib/test-seeks.c new file mode 100644 index 00000000..c6abf2c1 --- /dev/null +++ b/tests/lib/test-seeks.c @@ -0,0 +1,167 @@ +/* + * test-seeks.c + * + * Lib BabelTrace - Seeks test program + * + * Copyright 2012 - Yannick Brosseau + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; under version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#define _GNU_SOURCE +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "common.h" +#include "tap.h" + +#define NR_TESTS 11 + +void run_seek_begin(char *path, uint64_t expected_begin) +{ + struct bt_context *ctx; + struct bt_ctf_iter *iter; + struct bt_ctf_event *event; + struct bt_iter_pos newpos; + int ret; + uint64_t timestamp_begin; + uint64_t timestamp_seek_begin; + + /* Open the trace */ + ctx = create_context_with_path(path); + if (!ctx) { + plan_skip_all("Cannot create valid context"); + } + + /* Create iterator with null begin and end */ + iter = bt_ctf_iter_create(ctx, NULL, NULL); + if (!iter) { + plan_skip_all("Cannot create valid iterator"); + } + + event = bt_ctf_iter_read_event(iter); + + ok(event, "Event valid"); + + /* Validate that the first timestamp is right */ + timestamp_begin = bt_ctf_get_timestamp(event); + + ok1(timestamp_begin == expected_begin); + + /* Validate that we get the same value after a seek begin */ + newpos.type = BT_SEEK_BEGIN; + ret = bt_iter_set_pos(bt_ctf_get_iter(iter), &newpos); + + ok(ret == 0, "Seek begin retval %d", ret); + + event = bt_ctf_iter_read_event(iter); + + ok(event, "Event valid"); + + timestamp_seek_begin = bt_ctf_get_timestamp(event); + + ok1(timestamp_begin == timestamp_seek_begin); + + bt_context_put(ctx); +} + + +void run_seek_last(char *path, uint64_t expected_last) +{ + struct bt_context *ctx; + struct bt_ctf_iter *iter; + struct bt_ctf_event *event; + struct bt_iter_pos newpos; + int ret; + uint64_t timestamp_last; + + /* Open the trace */ + ctx = create_context_with_path(path); + if (!ctx) { + plan_skip_all("Cannot create valid context"); + } + + /* Create iterator with null last and end */ + iter = bt_ctf_iter_create(ctx, NULL, NULL); + if (!iter) { + plan_skip_all("Cannot create valid iterator"); + } + + event = bt_ctf_iter_read_event(iter); + + ok(event, "Event valid at beginning"); + + /* Seek to last */ + newpos.type = BT_SEEK_LAST; + ret = bt_iter_set_pos(bt_ctf_get_iter(iter), &newpos); + + ok(ret == 0, "Seek last retval %d", ret); + + event = bt_ctf_iter_read_event(iter); + + ok(event, "Event valid at last position"); + + timestamp_last = bt_ctf_get_timestamp(event); + + ok1(timestamp_last == expected_last); + + /* Try to read next event */ + ret = bt_iter_next(bt_ctf_get_iter(iter)); + + ok(ret == 0, "iter next should return an error"); + + event = bt_ctf_iter_read_event(iter); + + ok(event == 0, "Event after last should be invalid"); + + bt_context_put(ctx); +} + +int main(int argc, char **argv) +{ + char *path; + uint64_t expected_begin; + uint64_t expected_last; + + plan_tests(NR_TESTS); + + if (argc < 4) { + plan_skip_all("Invalid arguments: need a trace path and the start and last timestamp"); + + } + + /* Parse arguments (Trace, begin timestamp) */ + path = argv[1]; + + expected_begin = strtoull(argv[2], NULL, 0); + if (ULLONG_MAX == expected_begin && errno == ERANGE) { + plan_skip_all("Invalid value for begin timestamp"); + } + + expected_last = strtoull(argv[3], NULL, 0); + if (ULLONG_MAX == expected_last && errno == ERANGE) { + plan_skip_all("Invalid value for last timestamp"); + } + + run_seek_begin(path, expected_begin); + run_seek_last(path, expected_last); + + return exit_status(); +} -- 2.34.1