[alohalytics] Moved unit test defines into a separate file.

This commit is contained in:
Alex Zolotarev 2015-06-23 00:47:43 +02:00
parent 41809a4d90
commit 5e2fb01ccf
3 changed files with 109 additions and 72 deletions

View file

@ -0,0 +1,89 @@
/*******************************************************************************
The MIT License (MIT)
Copyright (c) 2015 Alexander Zolotarev <me@alex.bio> from Minsk, Belarus
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/
#ifndef TEST_DEFINES_H
#define TEST_DEFINES_H
#include <cstdlib>
#include <exception>
#include <iostream>
#define TEST_EQUAL(x, y) \
{ \
auto vx = (x); \
auto vy = (y); \
if (vx != vy) { \
std::cerr << __FILE__ << ':' << __FUNCTION__ << ':' << __LINE__ << " Test failed: " << #x << " != " << #y \
<< " (" << vx << " != " << vy << ")" << std::endl; \
std::exit(-1); \
} \
}
#define TEST_ALMOST_EQUAL(x, y, epsilon) \
{ \
auto vx = (x); \
auto vy = (y); \
if (fabs(vx - vy) > epsilon) { \
cerr << "Test failed: " << #x << " ~!= " << #y << " (" << vx << " ~!= " << vy << ")" << endl; \
return -1; \
} \
}
#define TEST_EXCEPTION(ex, op) \
{ \
bool has_fired = false; \
try { \
op; \
} catch (const std::exception & exc) { \
has_fired = true; \
if (typeid(ex) != typeid(exc)) { \
std::cerr << __FILE__ << ':' << __FUNCTION__ << ':' << __LINE__ << " Test failed: " << typeid(ex).name() \
<< " != " << typeid(exc).name() << std::endl; \
std::exit(-1); \
} \
} \
if (!has_fired) { \
std::cerr << __FILE__ << ':' << __FUNCTION__ << ':' << __LINE__ << " Test failed: " \
<< "Exception " << typeid(ex).name() << "Was not thrown." << std::endl; \
std::exit(-1); \
} \
}
// Generates unique temporary file name or empty string on error.
inline static std::string GenerateTemporaryFileName() {
#ifdef _MSC_VER
char tmp_file[L_tmpnam];
if (0 == ::tmpnam_s(tmp_file, L_tmpnam)) {
return tmp_file;
}
#else
char tmp_file[] = "/tmp/alohalytics_file_manager-XXXXXX";
if (::mktemp(tmp_file)) {
return tmp_file;
}
#endif
return std::string();
}
#endif // TEST_DEFINES_H

View file

@ -21,6 +21,9 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/
#include "test_defines.h"
#include "../src/location.h"
#include <sstream>
@ -33,17 +36,6 @@ using std::cerr;
using std::endl;
using std::string;
#define ASSERT_EQUAL(x, y) \
if ((x) != (y)) { \
cerr << "Test failed: " << #x << " != " << #y << " (" << (x) << " != " << (y) << ")" << endl; \
return -1; \
}
#define ASSERT_ALMOST_EQUAL(x, y, epsilon) \
if (fabs((x) - (y)) > epsilon) { \
cerr << "Test failed: " << #x << " ~!= " << #y << " (" << (x) << " ~!= " << (y) << ")" << endl; \
return -1; \
}
int main(int, char **) {
cerr.precision(20);
cerr << std::fixed;
@ -65,23 +57,23 @@ int main(int, char **) {
.SetBearing(bearing)
.SetSource(source)
.Encode();
ASSERT_EQUAL(serialized.size(), 32);
TEST_EQUAL(serialized.size(), std::string::size_type(32));
const alohalytics::Location l(serialized);
ASSERT_EQUAL(l.HasLatLon(), true);
ASSERT_EQUAL(l.timestamp_ms_, timestamp);
ASSERT_ALMOST_EQUAL(l.latitude_deg_, lat, 1e-7);
ASSERT_ALMOST_EQUAL(l.longitude_deg_, lon, 1e-7);
ASSERT_ALMOST_EQUAL(l.horizontal_accuracy_m_, horizontal_accuracy, 1e-2);
ASSERT_EQUAL(l.HasAltitude(), true);
ASSERT_ALMOST_EQUAL(l.altitude_m_, alt, 1e-2);
ASSERT_ALMOST_EQUAL(l.vertical_accuracy_m_, vertical_accuracy, 1e-2);
ASSERT_EQUAL(l.HasBearing(), true);
ASSERT_ALMOST_EQUAL(l.bearing_deg_, bearing, 1e-7);
ASSERT_EQUAL(l.HasSpeed(), true);
ASSERT_ALMOST_EQUAL(l.speed_mps_, speed, 1e-2);
ASSERT_EQUAL(l.HasSource(), true);
ASSERT_EQUAL(l.source_, source);
TEST_EQUAL(l.HasLatLon(), true);
TEST_EQUAL(l.timestamp_ms_, timestamp);
TEST_ALMOST_EQUAL(l.latitude_deg_, lat, 1e-7);
TEST_ALMOST_EQUAL(l.longitude_deg_, lon, 1e-7);
TEST_ALMOST_EQUAL(l.horizontal_accuracy_m_, horizontal_accuracy, 1e-2);
TEST_EQUAL(l.HasAltitude(), true);
TEST_ALMOST_EQUAL(l.altitude_m_, alt, 1e-2);
TEST_ALMOST_EQUAL(l.vertical_accuracy_m_, vertical_accuracy, 1e-2);
TEST_EQUAL(l.HasBearing(), true);
TEST_ALMOST_EQUAL(l.bearing_deg_, bearing, 1e-7);
TEST_EQUAL(l.HasSpeed(), true);
TEST_ALMOST_EQUAL(l.speed_mps_, speed, 1e-2);
TEST_EQUAL(l.HasSource(), true);
TEST_EQUAL(l.source_, source);
std::cout << "All tests have passed." << endl;
return 0;

View file

@ -22,6 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/
#include "test_defines.h"
#include "../src/file_manager.h"
#include "../src/messages_queue.h"
@ -35,55 +37,9 @@ SOFTWARE.
#include <thread>
#include <vector>
#define TEST_EQUAL(x, y) \
{ \
auto vx = (x); \
auto vy = (y); \
if (vx != vy) { \
std::cerr << __FILE__ << ':' << __FUNCTION__ << ':' << __LINE__ << " Test failed: " << #x << " != " << #y \
<< " (" << vx << " != " << vy << ")" << std::endl; \
std::exit(-1); \
} \
}
#define TEST_EXCEPTION(ex, op) \
{ \
bool has_fired = false; \
try { \
op; \
} catch (const std::exception & exc) { \
has_fired = true; \
if (typeid(ex) != typeid(exc)) { \
std::cerr << __FILE__ << ':' << __FUNCTION__ << ':' << __LINE__ << " Test failed: " << typeid(ex).name() \
<< " != " << typeid(exc).name() << std::endl; \
std::exit(-1); \
} \
} \
if (!has_fired) { \
std::cerr << __FILE__ << ':' << __FUNCTION__ << ':' << __LINE__ << " Test failed: " \
<< "Exception " << typeid(ex).name() << "Was not thrown." << std::endl; \
std::exit(-1); \
} \
}
using alohalytics::FileManager;
using alohalytics::ScopedRemoveFile;
// Generates unique temporary file name or empty string on error.
static std::string GenerateTemporaryFileName() {
#ifdef _MSC_VER
char tmp_file[L_tmpnam];
if (0 == ::tmpnam_s(tmp_file, L_tmpnam)) {
return tmp_file;
}
#else
char tmp_file[] = "/tmp/alohalytics_file_manager-XXXXXX";
if (::mktemp(tmp_file)) {
return tmp_file;
}
#endif
return std::string();
}
void Test_GetDirectoryFromFilePath() {
const std::string s = std::string(1, FileManager::kDirectorySeparator);