strncpy and move testing to seperate file

This commit is contained in:
PabstMirror 2019-09-30 22:16:13 -05:00
parent 687663b4ed
commit 126c8d3487
4 changed files with 143 additions and 89 deletions

View File

@ -10,3 +10,15 @@ if(CMAKE_COMPILER_IS_GNUCXX)
set_target_properties(${ACE_EXTENSION_NAME} PROPERTIES LINK_SEARCH_START_STATIC 1) set_target_properties(${ACE_EXTENSION_NAME} PROPERTIES LINK_SEARCH_START_STATIC 1)
set_target_properties(${ACE_EXTENSION_NAME} PROPERTIES LINK_SEARCH_END_STATIC 1) set_target_properties(${ACE_EXTENSION_NAME} PROPERTIES LINK_SEARCH_END_STATIC 1)
endif() endif()
# enable_testing()
# set(ACE_TEST_NAME Test_${ACE_EXTENSION_NAME})
# add_executable(${ACE_TEST_NAME} tests/tester.cpp ${SOURCES})
# target_link_libraries(${ACE_TEST_NAME} ace_common)
# target_link_libraries(${ACE_TEST_NAME} ${ACE_EXTENSION_NAME})
# target_link_libraries(${ACE_TEST_NAME} gtest_main)
# add_test(${ACE_TEST_NAME} ${ACE_TEST_NAME})
# set_target_properties(${ACE_TEST_NAME} PROPERTIES FOLDER Tests)

View File

@ -1,33 +1,14 @@
/* /*
* ace_artillerytables.cpp * artillerytables.hpp
* Author: PabstMirror * Author: PabstMirror
*/ */
//#define TEST_EXE
#define _USE_MATH_DEFINES
#include <cmath>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip> #include <iomanip>
#include <fstream>
#include <tuple>
#include <algorithm>
#include <chrono>
#include <future> #include <future>
#include <sstream>
// ace libs: #include "artillerytables.hpp"
#include "vector.hpp"
#ifndef TEST_EXE
extern "C" {
__declspec(dllexport) void __stdcall RVExtension(char* output, int outputSize, const char* function);
__declspec(dllexport) int __stdcall RVExtensionArgs(char* output, int outputSize, const char* function, const char** argv, int argc);
__declspec(dllexport) void __stdcall RVExtensionVersion(char* output, int outputSize);
}
#endif
// Constants // Constants
static const double timeStep = 1.0 / 60; static const double timeStep = 1.0 / 60;
@ -223,18 +204,15 @@ std::string simulateCalcRangeTableLine(const double _rangeToHit, const double _m
return (returnSS.str()); return (returnSS.str());
} }
#ifndef ACE_FULL_VERSION_STR
#define ACE_FULL_VERSION_STR "not defined"
#endif
void __stdcall RVExtensionVersion(char* output, int outputSize) { void __stdcall RVExtensionVersion(char* output, int outputSize) {
strncpy_s(output, outputSize, ACE_FULL_VERSION_STR, _TRUNCATE); strncpy(output, ACE_FULL_VERSION_STR, outputSize);
} }
void __stdcall RVExtension(char* output, int outputSize, const char* function) { void __stdcall RVExtension(char* output, int outputSize, const char* function) {
if (!strcmp(function, "version")) { if (!strcmp(function, "version")) {
RVExtensionVersion(output, outputSize); RVExtensionVersion(output, outputSize);
return; return;
} }
strncpy_s(output, outputSize, "error", _TRUNCATE); strncpy(output, "error - use args version of callExtension", outputSize);
} }
int __stdcall RVExtensionArgs(char* output, int outputSize, const char* function, const char** args, int argsCnt) { int __stdcall RVExtensionArgs(char* output, int outputSize, const char* function, const char** args, int argsCnt) {
if (!strcmp(function, "version")) { if (!strcmp(function, "version")) {
@ -276,7 +254,7 @@ int __stdcall RVExtensionArgs(char* output, int outputSize, const char* function
std::stringstream outputStr; // debug max distance and thead count std::stringstream outputStr; // debug max distance and thead count
outputStr << "[" << bestDistance << "," << fWorkers.size() << "]"; outputStr << "[" << bestDistance << "," << fWorkers.size() << "]";
strncpy_s(output, outputSize, outputStr.str().c_str(), _TRUNCATE); strncpy(output, outputStr.str().c_str(), outputSize);
return 0; return 0;
} }
@ -296,68 +274,10 @@ int __stdcall RVExtensionArgs(char* output, int outputSize, const char* function
result = fWorkers[getLineIndex].get(); result = fWorkers[getLineIndex].get();
getLineIndex++; getLineIndex++;
} }
strncpy_s(output, outputSize, result.c_str(), _TRUNCATE); strncpy(output, result.c_str(), outputSize);
return 1; return 1;
} }
return -1; // Error: function not valid strncpy(output, "error - invalid function", outputSize);
return RETURN_INVALID_FUNCTION; // Error: function not valid
} }
#ifdef TEST_EXE
int main() {
/*
double x1, x2, y1, y2, tof1, tof2;
std::tie(x1, y1, tof1) = simulateFindSolution(5000, 0, 810, 0, 0, 45 * (M_PI / 180.0), false);
std::tie(x2, y2, tof2) = simulateFindSolution(5000, 0, 810, -0.00000000000000000001, 0, 45 * (M_PI / 180.0), false);
printf("calc %f, %f, %f\n", x1, y1, tof1);
printf("sim %f, %f, %f\n", x2, y2, tof2);
printf("sim diff %f, %f, %f\n", (x2 - x1), (y2 - y1), (tof2 - tof1));
//std::string r = simulateCalcRangeTableLine(4000, 810, );
//printf("result: [%s]\n", r.c_str());
*/
// Determine realistic air firiction values
/*
double mv = 241;
printf(" %f m/s\n", mv);
double range;
for (double ar = 0; ar > -0.00015; ar -= 0.00001) {
std::tie(std::ignore, range) = findMaxAngle(mv, ar);
printf("[%f] = %f\n", ar, range);
}
*/
// test callExtension
char output[256];
char function1[] = "start";
//const char* args1[] = { "200", "0", "-5", "80", "false" };
//const char* args1[] = { "153.9", "-0.00005", "-5", "80", "false" };
const char* args1[] = { "810", "-0.00005", "-5", "80", "false" };
//const char* args1[] = { "810", "0", "-5", "80", "true" };
auto t1 = std::chrono::high_resolution_clock::now();
int ret = RVExtensionArgs(output, 256, function1, args1, 5);
auto t2 = std::chrono::high_resolution_clock::now();
std::printf("ret: %d - %s\n", ret, output);
std::printf("func %s: %1.1f ms\n", function1, std::chrono::duration<double, std::milli>(t2 - t1).count());
int lines = 0;
auto t3 = std::chrono::high_resolution_clock::now();
char function2[] = "getline";
int ret2 = 0;
while (ret2 != 3) { // dumb spin
ret2 = RVExtensionArgs(output, 256, function2, NULL, 0);
if (ret2 == 1) {
lines++;
// std::printf("ret: %d - %s\n", ret2, output);
}
}
auto t4 = std::chrono::high_resolution_clock::now();
std::printf("func %s: %1.1f ms with %d lines\n", function2, std::chrono::duration<double, std::milli>(t4 - t3).count(), lines);
std::printf("callExtensions finished in %1.1f ms\n", std::chrono::duration<double, std::milli>(t4 - t1).count());
}
#endif

View File

@ -0,0 +1,22 @@
/*
* artillerytables.hpp
* Author: PabstMirror
*/
// ace libs:
#include "vector.hpp"
#include "shared.hpp"
#define RETURN_INVALID_FUNCTION -1001
#define RETURN_WRONG_ARG_COUNT -1002
extern "C" {
__declspec(dllexport) void __stdcall RVExtension(char* output, int outputSize, const char* function);
__declspec(dllexport) int __stdcall RVExtensionArgs(char* output, int outputSize, const char* function, const char** argv, int argc);
__declspec(dllexport) void __stdcall RVExtensionVersion(char* output, int outputSize);
}
std::tuple<double, double, double> simulateShot(const double _fireAngleRad, const double _muzzleVelocity, const double _heightOfTarget, const double _crossWind, const double _tailWind, const double _temperature, const double _airDensity, double _airFriction);
std::tuple<double, double> findMaxAngle(const double _muzzleVelocity, const double _airFriction);
std::tuple<double, double, double> simulateFindSolution(const double _rangeToHit, const double _heightToHit, const double _muzzleVelocity, const double _airFriction, const double _minElev, const double _maxElev, const bool _highArc);
void writeNumber(std::stringstream & ss, double _num, const int _widthInt, const int _widthDec);
std::string simulateCalcRangeTableLine(const double _rangeToHit, const double _muzzleVelocity, const double _airFriction, const double _minElev, const double _maxElev, const bool _highArc);

View File

@ -0,0 +1,100 @@
#include <iostream>
#include <chrono>
#include "gtest/gtest.h"
#include "../artillerytables.hpp"
extern "C" {
__declspec(dllexport) void __stdcall RVExtension(char* output, int outputSize, const char* function);
__declspec(dllexport) int __stdcall RVExtensionArgs(char* output, int outputSize, const char* function, const char** argv, int argc);
__declspec(dllexport) void __stdcall RVExtensionVersion(char* output, int outputSize);
}
namespace test_ace_artillerytables {
TEST(Extension, VersionOld) {
char output[256];
char function[] = "version";
RVExtension(output, 256, function);
std::cout << "VersionOld: " << output << "\n";
ASSERT_STREQ(output, ACE_FULL_VERSION_STR);
}
TEST(Extension, VersionRVExtensionVersion) {
char output[256];
RVExtensionVersion(output, 256);
std::cout << "VersionExtension: " << output << "\n";
ASSERT_STREQ(output, ACE_FULL_VERSION_STR);
}
TEST(Extension, VersionArray) {
char output[256];
char function[] = "version";
int extReturn = RVExtensionArgs(output, 256, function, NULL, 0);
std::cout << "VersionNew: " << output << "\n";
ASSERT_EQ(extReturn, 0);
ASSERT_STREQ(output, ACE_FULL_VERSION_STR);
}
TEST(Extension, InvalidFuncOld) {
char output[256];
char function[] = "blah";
RVExtension(output, 256, function);
ASSERT_STREQ(output, "error - use args version of callExtension");
}
TEST(Extension, InvalidFuncArray) {
char output[256];
char function[] = "blah";
int extReturn = RVExtensionArgs(output, 256, function, nullptr, 0);
std::cout << "InvalidFunc: " << output << "\n";
ASSERT_EQ(extReturn, RETURN_INVALID_FUNCTION);
ASSERT_STREQ(output, "error - invalid function");
}
TEST(Extension, TestRun) {
// very basic test that it runs the correct number of lines
char output[256];
// Start:
char function1[] = "start";
const char* args1[] = { "400", "-0.00005", "-5", "80", "true" };
auto t1 = std::chrono::high_resolution_clock::now();
int ret1 = RVExtensionArgs(output, 256, function1, args1, 5);
auto t2 = std::chrono::high_resolution_clock::now();
std::printf("ret: %d - %s\n", ret1, output);
std::printf("func %s: %1.1f ms\n", function1, std::chrono::duration<double, std::milli>(t2 - t1).count());
ASSERT_STREQ(output, "[10391.8,103]");
ASSERT_EQ(ret1, 0);
int lines = 0;
auto t3 = std::chrono::high_resolution_clock::now();
char function2[] = "getline";
int ret2 = 0;
while (ret2 != 3) { // dumb spin
ret2 = RVExtensionArgs(output, 256, function2, NULL, 0);
if (ret2 == 1) {
lines++;
// std::printf("ret: %d - %s\n", ret2, output);
}
}
auto t4 = std::chrono::high_resolution_clock::now();
std::printf("func %s: %1.1f ms with %d lines\n", function2, std::chrono::duration<double, std::milli>(t3 - t2).count(), lines);
std::printf("callExtensions finished in %1.1f ms\n", std::chrono::duration<double, std::milli>(t4 - t1).count());
ASSERT_EQ(lines, 69);
}
}
int main(int argc, char** argv) {
// Misc Testing code
// Determine realistic air firiction values
//double mv = 241;
//std::printf(" %f m/s\n", mv);
//double range;
//for (double ar = 0; ar > -0.00015; ar -= 0.00001) {
// std::tie(std::ignore, range) = findMaxAngle(mv, ar);
// printf("[%f] = %f\n", ar, range);
//}
::testing::InitGoogleTest(&argc, argv);
std::cout << "Starting tests!\n";
return RUN_ALL_TESTS();
}