Commit 46a6f424 authored by Kirill Smelkov's avatar Kirill Smelkov

libgolang: tests: Factor out common testing functionality into shared place

Currently libgolang_test.cpp contains tests for code in libgolang.cpp and
for code that lives in other libgolang packages - sync, fmt, etc. It is
becoming tight and we are going to split libgolang_test.cpp and move
package tests to their corresponing files - e.g. to sync_test.cpp and
the like.

Move common assertion utilities into shared header before that as a
preparatory step.
parent e028cf28
......@@ -18,6 +18,7 @@ include golang/sync.h
include golang/sync.cpp
include golang/time.h
include golang/time.cpp
include golang/_testing.h
recursive-include golang *.py *.pxd *.pyx *.toml *.txt
recursive-include gpython *.py
recursive-include 3rdparty *.h
#ifndef _NXD_LIBGOLANG__TESTING_H
#define _NXD_LIBGOLANG__TESTING_H
// Copyright (C) 2019-2020 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
// Package _testing provides internal bits for testing libgolang and
// accompanying packages.
#include "golang/libgolang.h"
#include <sstream>
#include <string.h>
#include <vector>
// std::to_string<T> - provide missing pieces.
namespace std {
using namespace golang;
// string -> string (not in STL, huh ?!)
string to_string(const string& s) { return s; }
// vector<T> -> string
template<typename T>
string to_string(const vector<T>& v) {
std::ostringstream ss;
ss << "[";
int i = 0;
for (auto x : v) {
if (i++ != 0)
ss << " ";
ss << x << ",";
}
ss << "]";
return ss.str();
}
} // std::
// golang::_testing::
namespace golang {
namespace _testing {
#define __STR(X) #X
#define STR(X) __STR(X)
#define ASSERT(COND) do { \
if (!(COND)) \
panic(__FILE__ ":" STR(__LINE__) " assert `" #COND "` failed"); \
} while(0)
#define ASSERT_EQ(A, B) golang::_testing::__assert_eq(#A, A, B)
template<typename T, typename U>
void __assert_eq(const string &expr, const T &have, const U &want) {
if (have != want) {
string emsg = expr + "\n";
emsg += "have: '" + std::to_string(have) + "'\n";
emsg += "want: '" + std::to_string(want) + "'";
panic(strdup(emsg.c_str())); // XXX strdup because panic just saves char* pointer
}
};
}} // golang::_testing::
#endif // _NXD_LIBGOLANG__TESTING_H
# Copyright (C) 2019 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com>
# Copyright (C) 2019-2020 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com>
#
# This program is free software: you can Use, Study, Modify and Redistribute
# it under the terms of the GNU General Public License version 3, or (at your
......@@ -180,6 +180,7 @@ def _with_build_defaults(kw): # -> (pygo, kw')
dependv.append('%s/golang/sync.h' % pygo)
dependv.append('%s/golang/time.h' % pygo)
dependv.append('%s/golang/pyx/runtime.h' % pygo)
dependv.append('%s/golang/_testing.h' % pygo)
kw['depends'] = dependv
return pygo, kw
......
......@@ -28,21 +28,15 @@
#include <stdio.h>
#include <tuple>
#include <utility>
#include <sstream>
#include <string.h>
#include <vector>
#include "golang/_testing.h"
using namespace golang;
using std::move;
using std::tie;
using std::vector;
#define __STR(X) #X
#define STR(X) __STR(X)
#define ASSERT(COND) do { \
if (!(COND)) \
panic(__FILE__ ":" STR(__LINE__) " assert `" #COND "` failed"); \
} while(0)
// verify chan<T> automatic reference counting.
void _test_chan_cpp_refcount() {
chan<int> ch;
......@@ -687,43 +681,6 @@ void _test_global() {
// ---- fmt:: ----
// std::
namespace std {
// string -> string (not in STL, huh ?!)
string to_string(const string& s) { return s; }
// vector<T> -> string
template<typename T>
string to_string(const vector<T>& v) {
std::ostringstream ss;
ss << "[";
int i = 0;
for (auto x : v) {
if (i++ != 0)
ss << " ";
ss << x << ",";
}
ss << "]";
return ss.str();
}
} // std::
template<typename T, typename U>
void __assert_eq(const string &expr, const T &have, const U &want) {
if (have != want) {
string emsg = expr + "\n";
emsg += "have: '" + std::to_string(have) + "'\n";
emsg += "want: '" + std::to_string(want) + "'";
panic(strdup(emsg.c_str())); // XXX strdup because panic just saves char* pointer
}
};
#define ASSERT_EQ(A, B) __assert_eq(#A, A, B)
void _test_fmt_sprintf_cpp() {
// NOTE not using vargs helper, since sprintf itself uses vargs and we want
// to test varg logic there for correctness too.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment