Skip to content

Commit

Permalink
tests: assert in compat tests even in release mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Jan 26, 2024
1 parent ec4b572 commit a894c22
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
19 changes: 15 additions & 4 deletions tests/compat_testutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Copyright (c) 2013 Insollo Entertainment, LLC. All rights reserved.
Copyright 2016 Franklin "Snaipe" Mathieu <[email protected]>
Copyright 2018 Capitar IT Group BV <[email protected]>
Copyright 2022 Staysail Systems, Inc. <[email protected]>
Copyright 2024 Staysail Systems, Inc. <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
Expand All @@ -27,13 +27,13 @@
// it for validating the compatibility features of nanomsg. As much as
// possible we want to run tests from the nanomsg test suite unmodified.

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <nng/compat/nanomsg/nn.h>
#include "compat_testutil.h"
#include <nng/compat/nanomsg/nn.h>

int test_socket_impl(char *file, int line, int family, int protocol);
int test_connect_impl(char *file, int line, int sock, char *address);
Expand Down Expand Up @@ -143,7 +143,7 @@ test_recv_impl(char *file, int line, int sock, char *data)
{
size_t data_len;
int rc;
char * buf;
char *buf;

data_len = strlen(data);
/* We allocate plus one byte so that we are sure that message received
Expand Down Expand Up @@ -227,3 +227,14 @@ nn_sleep(int ms)
{
nng_msleep(ms);
}

void
nn_assert_impl(bool b, const char *expression, const char *file, int line)
{
if (b) {
return;
}
fprintf(
stderr, "%s:%d: Assertion failed: %s\n", file, line, expression);
abort();
}
25 changes: 14 additions & 11 deletions tests/compat_testutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright 2017 Garrett D'Amore <[email protected]>
Copyright 2016 Franklin "Snaipe" Mathieu <[email protected]>
Copyright 2018 Capitar IT Group BV <[email protected]>
Copyright 2021 Staysail Systems, Inc. <[email protected]>
Copyright 2024 Staysail Systems, Inc. <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -31,24 +31,25 @@
#ifndef COMPAT_TESTUTIL_H_INCLUDED
#define COMPAT_TESTUTIL_H_INCLUDED

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define nn_err_strerror nn_strerror
#define nn_err_abort abort
#define nn_assert assert
#define errno_assert assert
#define wsa_assert assert
#define alloc_assert(x) assert((x) != NULL)
#define errno_assert nn_assert
#define wsa_assert nn_assert
#define alloc_assert(x) nn_assert((x) != NULL)

#if defined __GNUC__ || defined __llvm__ || defined __clang__
#define NN_UNUSED __attribute__((unused))
#else
#define NN_UNUSED
#endif

#define nn_assert(x) nn_assert_impl(x, #x, __FILE__, __LINE__)

extern int test_socket_impl(char *file, int line, int family, int protocol);
extern int test_connect_impl(char *file, int line, int sock, char *address);
extern int test_bind_impl(char *file, int line, int sock, char *address);
Expand All @@ -57,11 +58,13 @@ extern void test_send_impl(char *file, int line, int sock, char *data);
extern void test_recv_impl(char *file, int line, int sock, char *data);
extern void test_drop_impl(char *file, int line, int sock, int err);
extern int test_setsockopt_impl(char *file, int line, int sock, int level,
int option, const void *optval, size_t optlen);
extern int get_test_port(int argc, const char *argv[]);
extern void test_addr_from(char *out, const char *proto, const char *ip,
int port);
int option, const void *optval, size_t optlen);
extern int get_test_port(int argc, const char *argv[]);
extern void test_addr_from(
char *out, const char *proto, const char *ip, int port);
extern void nn_sleep(int);
extern void nn_assert_impl(
bool b, const char *expression, const char *file, int line);

#define test_socket(f, p) test_socket_impl(__FILE__, __LINE__, (f), (p))
#define test_connect(s, a) test_connect_impl(__FILE__, __LINE__, (s), (a))
Expand All @@ -77,7 +80,7 @@ struct nn_thread {
void *thr;
};

extern int nn_thread_init(struct nn_thread *, void (*)(void *), void *);
extern int nn_thread_init(struct nn_thread *, void (*)(void *), void *);
extern void nn_thread_term(struct nn_thread *);

#endif // COMPAT_TESTUTIL_H_INCLUDED

0 comments on commit a894c22

Please sign in to comment.