2015-05-04 22:25:41 +04:00
|
|
|
#include "cppunitlite/Assertions.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-15 23:20:04 +04:00
|
|
|
#include <stdlib.h>
|
2015-05-04 22:25:41 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
void Assertions::StringEquals(std::string message, std::string expected, std::string actual, const char* fileName, long lineNumber) {
|
|
|
|
if (expected != actual) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << message << " (expected '" << expected << "', got '" << actual << "')";
|
|
|
|
throw TestFailException(ss.str(), std::string(fileName), lineNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Assertions::StringEquals(std::string message, const char* expected, const char* actual, const char* fileName, long lineNumber) {
|
2015-06-27 13:36:55 +04:00
|
|
|
if (actual == NULL) {
|
2015-05-04 22:25:41 +04:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << message << " (expected '" << expected << "', got NULL";
|
|
|
|
throw TestFailException(ss.str(), std::string(fileName), lineNumber);
|
|
|
|
}
|
|
|
|
if (strcmp(expected, actual)) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << message << " (expected '" << expected << "', got '" << actual << "')";
|
|
|
|
throw TestFailException(ss.str(), std::string(fileName), lineNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 19:43:31 +06:00
|
|
|
void Assertions::ConditionFailed(std::string message, std::string condition, const char* fileName, long lineNumber, bool onlyWarning) {
|
2015-05-04 22:25:41 +04:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << message << " (condition failed: " << condition << ")";
|
2015-09-28 19:43:31 +06:00
|
|
|
throw TestFailException(ss.str(), std::string(fileName), lineNumber, onlyWarning);
|
2015-05-04 22:25:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Assertions::LongEquals(std::string message, long expected, long actual, const char* fileName, long lineNumber) {
|
|
|
|
if (expected != actual) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << message << " (expected '" << expected << "', got '" << actual << "')";
|
|
|
|
throw TestFailException(ss.str(), std::string(fileName), lineNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Assertions::UInt32Equals(std::string message, unsigned int expected, unsigned int actual, const char* fileName, long lineNumber) {
|
|
|
|
if (expected != actual) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << message << " (expected '" << expected << "', got '" << actual << "')";
|
|
|
|
throw TestFailException(ss.str(), std::string(fileName), lineNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Assertions::CharEquals(std::string message, char expected, char actual, const char* fileName, long lineNumber) {
|
|
|
|
if (expected != actual) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << message << " (expected '" << expected << "', got '" << actual << "')";
|
|
|
|
throw TestFailException(ss.str(), std::string(fileName), lineNumber);
|
|
|
|
}
|
2015-05-15 23:20:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Assertions::DoubleEquals(std::string message, double expected, double actual, double epsilon, const char* fileName, long lineNumber) {
|
|
|
|
if (abs(expected - actual) > epsilon) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << message << " (expected '" << expected << "', got '" << actual << "')";
|
|
|
|
throw TestFailException(ss.str(), std::string(fileName), lineNumber);
|
|
|
|
}
|
|
|
|
}
|
2015-06-13 22:10:36 +04:00
|
|
|
|
|
|
|
void Assertions::MemoryEquals(std::string message, void* expected, void* actual, int size, const char* fileName, long lineNumber) {
|
|
|
|
if (memcmp(expected, actual, size)) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << message << " (expected '";
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
unsigned char b = *((unsigned char*)expected + i);
|
|
|
|
ss << std::hex << (unsigned int) b << " ";
|
|
|
|
}
|
|
|
|
ss << "', got '";
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
unsigned char b = *((unsigned char*)actual + i);
|
|
|
|
ss << std::hex << (unsigned int)b << " ";
|
|
|
|
}
|
|
|
|
ss << "')";
|
|
|
|
throw TestFailException(ss.str(), std::string(fileName), lineNumber);
|
|
|
|
}
|
|
|
|
}
|