Skip to content
This repository has been archived by the owner on Jan 26, 2024. It is now read-only.

Commit

Permalink
Ensure that assert is not ignored for tests. (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
rnburn authored Nov 29, 2017
1 parent f645d31 commit d901cf0
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ TEST_NAMES = [

[cc_test(
name = test_name,
srcs = [test_name + ".cpp"],
srcs = [test_name + ".cpp", "assert.h"],
deps = ["//:opentracing"],
) for test_name in TEST_NAMES]
13 changes: 13 additions & 0 deletions test/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <iostream>
#include <cstdlib>

#define ASSERT(EXPR) \
do { \
if (!(EXPR)) { \
std::cerr << "Failed in " << __FILE__ << ":" << __LINE__ << ": \"" \
<< #EXPR "\" evaluted to false\n"; \
std::exit(-1); \
} \
} while (false);
19 changes: 10 additions & 9 deletions test/string_view_test.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
#include "assert.h"

#include <opentracing/string_view.h> // test include guard
#include <opentracing/string_view.h>
#include <cassert>

using namespace opentracing;

static void test_empty() {
string_view ref;
assert(0 == ref.data());
assert(0 == ref.length());
ASSERT(0 == ref.data());
ASSERT(0 == ref.length());
}

static void test_cstring() {
const char* val = "hello world";

string_view ref(val);

assert(val == ref.data());
assert(std::strlen(val) == ref.length());
ASSERT(val == ref.data());
ASSERT(std::strlen(val) == ref.length());
}

static void test_std_string() {
const std::string val = "hello world";

string_view ref(val);

assert(val == ref.data());
assert(val.length() == ref.length());
ASSERT(val == ref.data());
ASSERT(val.length() == ref.length());
}

static void test_copy() {
Expand All @@ -34,8 +35,8 @@ static void test_copy() {
string_view ref(val);
string_view cpy(ref);

assert(val == cpy.data());
assert(val.length() == cpy.length());
ASSERT(val == cpy.data());
ASSERT(val.length() == cpy.length());
}

int main() {
Expand Down
13 changes: 7 additions & 6 deletions test/tracer_test.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
#include "assert.h"

#include <opentracing/noop.h>
#include <opentracing/tracer.h>
#include <cassert>
using namespace opentracing;

static void test_tracer_interface() {
auto tracer = MakeNoopTracer();

auto span1 = tracer->StartSpan("a");
assert(span1);
assert(&span1->tracer() == tracer.get());
ASSERT(span1);
ASSERT(&span1->tracer() == tracer.get());

auto span2 = tracer->StartSpan("b", {ChildOf(&span1->context())});
assert(span2);
ASSERT(span2);
span2->SetOperationName("b1");
span2->SetTag("x", true);
assert(span2->BaggageItem("y").empty());
ASSERT(span2->BaggageItem("y").empty());
span2->Log({{"event", "xyz"}, {"abc", 123}});
span2->Finish();
}
Expand All @@ -24,7 +25,7 @@ static void test_start_span_options() {

// A reference to null a SpanContext is ignored.
ChildOf(nullptr).Apply(options);
assert(options.references.size() == 0);
ASSERT(options.references.size() == 0);
}

int main() {
Expand Down
5 changes: 3 additions & 2 deletions test/util_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "assert.h"

#include <opentracing/util.h>
#include <cassert>
#include <cmath>
#include <cstdlib> // Work around to https://stackoverflow.com/a/30084734.
using namespace opentracing;
Expand All @@ -13,7 +14,7 @@ static void test_convert_time_point() {
auto t3 = convert_time_point<SystemClock>(t2);
auto difference = std::abs(
std::chrono::duration_cast<std::chrono::microseconds>(t3 - t1).count());
assert(difference < 100);
ASSERT(difference < 100);
}

int main() {
Expand Down
17 changes: 9 additions & 8 deletions test/value_test.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
#include "assert.h"

#include <opentracing/value.h>
#include <cassert>
using namespace opentracing;

static void test_implicit_construction() {
Value v1(123);
assert(v1.is<int64_t>());
ASSERT(v1.is<int64_t>());

Value v2(123u);
assert(v2.is<uint64_t>());
ASSERT(v2.is<uint64_t>());

Value v3(true);
assert(v3.is<bool>());
ASSERT(v3.is<bool>());

Value v4(1.0);
assert(v4.is<double>());
ASSERT(v4.is<double>());
Value v5(1.0f);
assert(v5.is<double>());
ASSERT(v5.is<double>());

Value v6(std::string("abc"));
assert(v6.is<std::string>());
ASSERT(v6.is<std::string>());

Value v7("abc");
assert(v7.is<const char*>());
ASSERT(v7.is<const char*>());

Value v8(Values{Value(1), Value(2)});
(void)v8;
Expand Down

0 comments on commit d901cf0

Please sign in to comment.