Commit c252d841 authored by Christoph Richter's avatar Christoph Richter
Browse files

Added tests for float division (#19)

parent c7f40fd8
Pipeline #67320 passed with stage
in 3 minutes and 7 seconds
......@@ -93,4 +93,20 @@ public class GenerateCMakeTest extends AbstractSymtabTest {
testFilesAreEqual(testFiles, restPath + "test/");
}
@Test
public void floatDivisionTest() throws IOException {
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("test.math.floatDivisionTest", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.useArmadilloBackend();
generatorCPP.setGenerateCMake(true);
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/cmake/test/math/FloatDivisionTest");
generatorCPP.setModelsDirPath(Paths.get("src/test/resources/"));
generatorCPP.setGenerateTests(true);
generatorCPP.setCheckModelDir(true);
List<File> files = generatorCPP.generateFiles(componentSymbol, symtab);
String restPath = "cmake/test/math/FloatDivisionTest/";
testCMakeFilesEqual(files, restPath);
}
}
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
project(test_math_floatDivisionTest LANGUAGES CXX)
#set cmake module path
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
# add dependencies
find_package(Armadillo REQUIRED)
set(INCLUDE_DIRS ${INCLUDE_DIRS} ${Armadillo_INCLUDE_DIRS})
set(LIBS ${LIBS} ${Armadillo_LIBRARIES})
# additional commands
# create static library
include_directories(${INCLUDE_DIRS})
add_library(test_math_floatDivisionTest test_math_floatDivisionTest.h)
target_include_directories(test_math_floatDivisionTest PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(test_math_floatDivisionTest PUBLIC ${LIBS})
set_target_properties(test_math_floatDivisionTest PROPERTIES LINKER_LANGUAGE CXX)
# export cmake project
export(TARGETS test_math_floatDivisionTest FILE test_math_floatDivisionTest.cmake)
# additional commands end
include_directories(test)
add_executable(test_math_floatDivisionTest_StreamTests test/tests_main.cpp)
target_compile_definitions(test_math_floatDivisionTest_StreamTests PRIVATE CATCH_CONFIG_MAIN=1 ARMA_DONT_USE_WRAPPER)
target_link_libraries(test_math_floatDivisionTest_StreamTests PUBLIC test_math_floatDivisionTest)
set_target_properties(test_math_floatDivisionTest_StreamTests PROPERTIES LINKER_LANGUAGE CXX)
#ifndef HELPERA_H
#define HELPERA_H
#include <iostream>
#include "armadillo.h"
#include <stdarg.h>
#include <initializer_list>
using namespace arma;
class HelperA{
public:
static mat getEigenVectors(mat A){
vec eigenValues;
mat eigenVectors;
eig_sym(eigenValues,eigenVectors,A);
return eigenVectors;
}
static vec getEigenValues(mat A){
vec eigenValues;
mat eigenVectors;
eig_sym(eigenValues,eigenVectors,A);
return eigenValues;
}
static mat getKMeansClusters(mat A, int k){
mat clusters;
kmeans(clusters,A.t(),k,random_subset,20,true);
/*printf("cluster centroid calculation done\n");
std::ofstream myfile;
myfile.open("data after cluster.txt");
myfile << A;
myfile.close();
std::ofstream myfile2;
myfile2.open("cluster centroids.txt");
myfile2 << clusters;
myfile2.close();*/
mat indexedData=getKMeansClustersIndexData(A.t(), clusters);
/*std::ofstream myfile3;
myfile3.open("data after index.txt");
myfile3 << indexedData;
myfile3.close();
*/
return indexedData;
}
static mat getKMeansClustersIndexData(mat A, mat centroids){
mat result=mat(A.n_cols, 1);
for(int i=0;i<A.n_cols;++i){
result(i, 0) = getIndexForClusterCentroids(A, i, centroids);
}
return result;
}
static int getIndexForClusterCentroids(mat A, int colIndex, mat centroids){
int index=1;
double lowestDistance=getEuclideanDistance(A, colIndex, centroids, 0);
for(int i=1;i<centroids.n_cols;++i){
double curDistance=getEuclideanDistance(A, colIndex, centroids, i);
if(curDistance<lowestDistance){
lowestDistance=curDistance;
index=i+1;
}
}
return index;
}
static double getEuclideanDistance(mat A, int colIndexA, mat B, int colIndexB){
double distance=0;
for(int i=0;i<A.n_rows;++i){
double elementA=A(i,colIndexA);
double elementB=B(i,colIndexB);
double diff=elementA-elementB;
distance+=diff*diff;
}
return sqrt(distance);
}
static mat getSqrtMat(mat A){
cx_mat result=sqrtmat(A);
return real(result);
}
static mat getSqrtMatDiag(mat A){
for(int i=0;i<A.n_rows;++i){
double curVal = A(i,i);
A(i,i) = sqrt(curVal);
}
return A;
}
static mat invertDiagMatrix(mat A){
for(int i=0;i<A.n_rows;++i){
double curVal = A(i,i);
A(i,i) = 1/curVal;
}
return A;
}
};
#endif
# Automatically generated file
#
# - Try to find Armadillo
# Once done this will define
# Armadillo_FOUND - System has Armadillo
# Armadillo_INCLUDE_DIRS - The Armadillo include directories
# Armadillo_LIBRARY_DIRS - The library directories needed to use Armadillo
# Armadillo_LIBRARIES - The libraries needed to use Armadillo
find_path(Armadillo_INCLUDE_DIR
NAMES armadillo
PATH_SUFFIXES "include"
PATHS
HINTS $ENV{Armadillo_HOME}
)
find_library(Armadillo_LIBRARY
NAMES armadillo
PATH_SUFFIXES "lib" "lib64" "lib/x86_64-linux-gnu" "examples/lib_win64" "build" "Release"
PATHS
HINTS $ENV{Armadillo_HOME}
)
include(FindPackageHandleStandardArgs)
# if all listed variables are TRUE
find_package_handle_standard_args(
Armadillo
DEFAULT_MSG
Armadillo_INCLUDE_DIR
Armadillo_LIBRARY
)
mark_as_advanced(
Armadillo_INCLUDE_DIR
Armadillo_LIBRARY
)
set(Armadillo_INCLUDE_DIRS ${Armadillo_INCLUDE_DIR})
set(Armadillo_LIBRARIES ${Armadillo_LIBRARY})
\ No newline at end of file
#ifndef TEST_MATH_FLOATDIVISIONTEST_TEST
#define TEST_MATH_FLOATDIVISIONTEST_TEST
#include "catch.hpp"
#include "../test_math_floatDivisionTest.h"
TEST_CASE("test.math.FloatDivisionTest", "[test_math_floatDivisionTest]") {
test_math_floatDivisionTest component;
component.init();
component.execute();
REQUIRE( component.out1 >= 0.4999 );
REQUIRE( component.out1 <= 0.5001 );
std::cout << "test.math.FloatDivisionTest: success\n";
}
#endif
#ifndef TESTS_MAIN
#define TESTS_MAIN
//#define CATCH_CONFIG_RUNNER
#define CATCH_CONFIG_FAST_COMPILE
#include "catch.hpp"
/*
int main(int argc, char* argv[]) {
Catch::Session session;
int returnCode = session.applyCommandLine(argc, argv);
if (returnCode != 0) {
return returnCode;
}
int numFailed = session.run();
return numFailed;
}*/
#include "test_math_floatDivisionTest_test.hpp"
#endif
#ifndef TEST_MATH_FLOATDIVISIONTEST
#define TEST_MATH_FLOATDIVISIONTEST
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "armadillo.h"
using namespace arma;
class test_math_floatDivisionTest{
public:
double out1;
mat out2;
mat CONSTANTCONSTANTVECTOR0;
void init()
{
out2=mat(2,2);
CONSTANTCONSTANTVECTOR0 = mat(2,2);
CONSTANTCONSTANTVECTOR0(0,0) = 1.0/4.0;
CONSTANTCONSTANTVECTOR0(0,1) = 1.0/4.0;
CONSTANTCONSTANTVECTOR0(1,0) = 1.0/4.0;
CONSTANTCONSTANTVECTOR0(1,1) = 1.0/4.0;
}
void execute()
{
out1 = 1.0/2.0;
mat A = CONSTANTCONSTANTVECTOR0;
out2 = A;
}
};
#endif
package test.math;
component FloatDivisionTest{
ports out Q out1,
out Q^{2, 2} out2;
implementation Math{
// scalar division
out1 = 1 / 2;
// matrix definition
Q^{2, 2} A = [1/4, 1/4; 1/4, 1/4];
out2 = A;
}
}
\ No newline at end of file
package test.math;
stream FloatDivisionTest for FloatDivisionTest{
out1: 0.5 +/- 0.0001;
// out2: [0.25 +/- 0.0001, 0.25 +/- 0.0001; 0.25 +/- 0.0001, 0.25 +/- 0.0001];
}
\ No newline at end of file
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