/* fuzzylite (R), a fuzzy logic control library in C++. Copyright (C) 2010-2017 FuzzyLite Limited. All rights reserved. Author: Juan Rada-Vilela, Ph.D. This file is part of fuzzylite. fuzzylite is free software: you can redistribute it and/or modify it under the terms of the FuzzyLite License included with the software. You should have received a copy of the FuzzyLite License along with fuzzylite. If not, see . fuzzylite is a registered trademark of FuzzyLite Limited. */ #include "fl/Benchmark.h" #include "catch2/catch.hpp" #include "fl/Headers.h" #include #include namespace fl { TEST_CASE("Benchmarks run from Console ", "[benchmark][console]") { // const char* args[] = {"dummy-command", "benchmarks", "../../examples/", "1"}; // Console::main(4, args); } TEST_CASE("Benchmarks from FLD files", "[benchmark][fld]") { std::string path = "../../examples/"; typedef std::pair Example; std::vector examples; examples.push_back(Example("mamdani/AllTerms", int(1e4))); examples.push_back(Example("mamdani/SimpleDimmer", int(1e5))); examples.push_back(Example("mamdani/octave/investment_portfolio", 256)); examples.push_back(Example("mamdani/octave/mamdani_tip_calculator", 256)); examples.push_back(Example("takagi-sugeno/approximation", int(1e6))); examples.push_back(Example("takagi-sugeno/SimpleDimmer", int(2e6))); examples.push_back(Example("takagi-sugeno/octave/cubic_approximator", int(2e6))); examples.push_back(Example("takagi-sugeno/octave/heart_disease_risk", 1024)); examples.push_back(Example("takagi-sugeno/octave/linear_tip_calculator", 1024)); examples.push_back(Example("takagi-sugeno/octave/sugeno_tip_calculator", 512)); examples.push_back(Example("tsukamoto/tsukamoto", int(1e6))); std::ostringstream writer; std::vector errors = std::vector(examples.size(), 0); for (std::size_t i = 0; i < examples.size(); ++i) { Example example = examples.at(i); FL_LOG("Benchmark " << (i + 1) << "/" << examples.size() << ": " << example.first << ".fll (" << example.second << " values)"); FL_unique_ptr engine(FllImporter().fromFile(path + example.first + ".fll")); #ifdef FL_USE_FLOAT scalar tolerance = 1e-3; #else scalar tolerance = fuzzylite::macheps(); #endif Benchmark benchmark(example.first, engine.get(), tolerance); std::ifstream reader(std::string(path + example.first + ".fld").c_str()); if (not reader.is_open()) { throw Exception("File not found: " + path + example.first + ".fld"); } benchmark.prepare(reader, 1024); benchmark.run(1); CHECK(benchmark.canComputeErrors() == true); errors.at(i) = benchmark.accuracyErrors(); if (i == 0) { writer << "\n" << benchmark.format(benchmark.results(), Benchmark::Horizontal, Benchmark::HeaderAndBody) << "\n"; } else { writer << benchmark.format(benchmark.results(), Benchmark::Horizontal, Benchmark::Body) << "\n"; } } FL_LOG(writer.str()); for (std::size_t i = 0; i < errors.size(); ++i) { FL_LOG("Checking for errors in: " << examples.at(i).first); CHECK(errors.at(i) == 0); } } TEST_CASE("Time conversions", "[benchmark][time]") { CHECK(Op::isEq(1.0, Benchmark::convert(3600, Benchmark::Seconds, Benchmark::Hours))); FL_LOG(Benchmark::convert(3600, Benchmark::Seconds, Benchmark::Hours)); CHECK(Op::isEq(3600, Benchmark::convert(1, Benchmark::Hours, Benchmark::Seconds))); FL_LOG(Benchmark::convert(1, Benchmark::Hours, Benchmark::Seconds)); CHECK(Op::isEq(1000.0, Benchmark::convert(1.0, Benchmark::Seconds, Benchmark::MilliSeconds))); FL_LOG(Benchmark::convert(1.0, Benchmark::Seconds, Benchmark::MilliSeconds)); CHECK(Op::isEq(1.0, Benchmark::convert(1000.0, Benchmark::MilliSeconds, Benchmark::Seconds))); FL_LOG(Benchmark::convert(1000.0, Benchmark::MilliSeconds, Benchmark::Seconds)); scalar eps = #ifndef __i386__ fuzzylite::macheps(); #else // on i386, due to the 80bit x87 register, double floating point // numbers are handled differently and thus the difference between // 35e9 and the result of Benchmark::convert() will be 2.179e-6, // which is greater than the default epsilon of 1e-6. 1e-5; #endif CHECK(Op::isEq(35e9, Benchmark::convert(35, Benchmark::Seconds, Benchmark::NanoSeconds), eps)); CHECK(Op::isEq(35, Benchmark::convert(35e9, Benchmark::NanoSeconds, Benchmark::Seconds))); } TEST_CASE("Benchmark headers", "[benchmark][header]") { FL_LOG(Op::join(Benchmark().header(10, true), "\t")); CHECK(Benchmark().header(10).size() == 30); FL_LOG(Op::join(Benchmark().header(10, false), "\t")); CHECK(Benchmark().header(10, false).size() == 30 - 8); } }