# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-BABELTRACE_BIN=@abs_top_builddir@/cli/babeltrace
+. "@abs_top_builddir@/tests/utils/common.sh"
-TRACE_PATH=@abs_top_srcdir@/tests/ctf-traces/succeed/wk-heartbeat-u/
+TRACE_PATH="${BT_CTF_TRACES}/succeed/wk-heartbeat-u/"
-source @abs_top_srcdir@/tests/utils/tap/tap.sh
-
-NUM_TESTS=10
+NUM_TESTS=40
plan_tests $NUM_TESTS
tmp_out=$(mktemp)
-$BABELTRACE_BIN --clock-gmt --begin 17:48:17.587029529 --end 17:48:17.588680018 \
- ${TRACE_PATH} >/dev/null 2>&1
-ok $? "Read the trace with the trimmer enabled"
+# Run Babeltrace with some command line arguments, verify exit status and
+# number of output events (i.e. number of output lines)
+#
+# Arguments:
+#
+# $1: expected number of events
+# $2: test description
+# remaining arguments: command-line arguments to pass to Babeltrace
+
+function expect_success()
+{
+ local expected_num_events="$1"
+ shift
+ local msg="$1"
+ shift
+
+ "${BT_BIN}" "${TRACE_PATH}" "$@" 2>/dev/null > "${tmp_out}"
+ ok $? "trimmer: ${msg}: exit status"
+ num_events=$(wc -l < "${tmp_out}")
+ # Use bash parameter expansion to strip spaces added by BSD 'wc' on macOs and Solaris
+ is "${num_events// /}" "${expected_num_events}" "trimmer: ${msg}: number of events (${expected_num_events})"
+}
-$BABELTRACE_BIN --clock-gmt --begin 17:48:17.587029529 ${TRACE_PATH} \
- 2>/dev/null >${tmp_out}
-ok $? "Running with --begin"
-cnt=$(cat ${tmp_out} | wc -l)
-test $cnt == 18
-ok $? "Expected number of events after trimming begin and end"
+expect_success 18 "--begin, GMT relative timestamps" \
+ --clock-gmt --begin 17:48:17.587029529
+expect_success 9 "--end, GMT relative timestamps" \
+ --clock-gmt --end 17:48:17.588680018
+expect_success 7 "--begin and --end, GMT relative timestamps" \
+ --clock-gmt --begin 17:48:17.587029529 --end 17:48:17.588680018
+expect_success 0 "--begin, out of range, GMT relative timestamps" \
+ --clock-gmt --begin 18:48:17.587029529
+expect_success 0 "--end, out of range, GMT relative timestamps" \
+ --clock-gmt --end 16:48:17.588680018
-$BABELTRACE_BIN --clock-gmt --end 17:48:17.588680018 ${TRACE_PATH} \
- 2>/dev/null >${tmp_out}
-ok $? "Running with --end"
-cnt=$(cat ${tmp_out} | wc -l)
-test $cnt == 9
-ok $? "Expected number of events after trimming end"
+expect_success 18 "--begin, GMT absolute timestamps" \
+ --clock-gmt --begin "2012-10-29 17:48:17.587029529"
+expect_success 9 "--end, GMT absolute timestamps" \
+ --clock-gmt --end "2012-10-29 17:48:17.588680018"
+expect_success 7 "--begin and --end, GMT absolute timestamps" \
+ --clock-gmt --begin "2012-10-29 17:48:17.587029529" --end "2012-10-29 17:48:17.588680018"
+expect_success 0 "--begin, out of range, GMT absolute timestamps" \
+ --clock-gmt --begin "2012-10-29 18:48:17.587029529"
+expect_success 0 "--begin, out of range, GMT absolute timestamps" \
+ --clock-gmt --end "2012-10-29 16:48:17.588680018"
-$BABELTRACE_BIN --clock-gmt --begin 17:48:17.587029529 --end 17:48:17.588680018 \
- ${TRACE_PATH} 2>/dev/null >${tmp_out}
-ok $? "Running with --begin and --end"
-cnt=$(cat ${tmp_out} | wc -l)
-test $cnt == 7
-ok $? "Expected number of events after trimming begin and end"
+export TZ=EST
-$BABELTRACE_BIN --clock-gmt --begin 18:48:17.587029529 ${TRACE_PATH} \
- 2>/dev/null >${tmp_out}
-ok $? "Running with --begin out of range"
-cnt=$(cat ${tmp_out} | wc -l)
-test $cnt == 0
-ok $? "No event output when begin is after the end of the trace"
+expect_success 18 "--begin, EST relative timestamps" \
+ --begin "12:48:17.587029529"
+expect_success 9 "--end, EST relative timestamps" \
+ --end "12:48:17.588680018"
+expect_success 7 "--begin and --end, EST relative timestamps" \
+ --begin "12:48:17.587029529" --end "12:48:17.588680018"
+expect_success 0 "--begin, out of range, EST relative timestamps" \
+ --begin "13:48:17.587029529"
+expect_success 0 "--end, out of range, EST relative timestamps" \
+ --end "11:48:17.588680018"
-$BABELTRACE_BIN --clock-gmt --end 16:48:17.588680018 ${TRACE_PATH} \
- 2>/dev/null >${tmp_out}
-cnt=$(cat ${tmp_out} | wc -l)
-test $cnt == 0
-ok $? "No event output when end is before the beginning of the trace"
+expect_success 18 "--begin, EST absolute timestamps" \
+ --begin "2012-10-29 12:48:17.587029529"
+expect_success 9 "--end, EST absolute timestamps" \
+ --end "12:48:17.588680018"
+expect_success 7 "--begin and --end, EST absolute timestamps" \
+ --begin "2012-10-29 12:48:17.587029529" --end "2012-10-29 12:48:17.588680018"
+expect_success 0 "--begin, out of range, EST absolute timestamps" \
+ --begin "2012-10-29 13:48:17.587029529"
+expect_success 0 "--end, out of range, EST absolute timestamps" \
+ --end "2012-10-29 11:48:17.588680018"
-rm ${tmp_out}
+rm "${tmp_out}"