GithubHelp home page GithubHelp logo

cpp-testing's Introduction

C++ Testing

This is just a simple project for making code testing easier, by utilizing object-oriented programming.

Example:

#include <iostream>

/**
 * Data type for running the tests.
 */
typedef unsigned int test_t;

/**
 * Number of times to run the test.
 */
constexpr test_t TEST_TIMES = 1'000;

/**
 * Iteration count for the loop.
 */
constexpr test_t ITERATIONS = 10'000;

/**
 * A class for testing the increment operator a certain number of times.
 *
 * @author Macintosh_Fan
 */
class IncrementTest : public ClockTest {
public:
    /**
     * Runs the test.
     */
    void run() override {
        start();
        for (test_t i = 0; i < ITERATIONS; i++, i++);
        end();
    }
};

/**
 * A class for testing the addition operator.
 * 
 * @author Macintosh_Fan
 */
class AddTest : public ClockTest {
public:
    /**
     * Runs the test.
     */
    void run() override {
        start();
        for (test_t i = 0; i < ITERATIONS; i += 2);
        end();
    }
};

/**
 * The main function.
 *
 * @return exit code
 * @author Macintosh_Fan
 */
int main() {
    IncrementTest incrementTest = IncrementTest();
    AddTest addTest = AddTest();

    test_t incrementTestScore = 0;
    test_t addTestScore = 0;
    for (test_t i = 0; i < TEST_TIMES; i++) {
        incrementTest.run();
        addTest.run();

        clock_t incrementTestTime = incrementTest.getTestTime();
        clock_t addTestTime = addTest.getTestTime();

        if (incrementTestTime < addTestTime) {
            std::cout << incrementTestTime << " < " << addTestTime << '\n';
            std::cout << "Increment beat add by " << (addTestTime - incrementTestTime) << '\n';
            std::cout << '\n';
            incrementTestScore++;
        } else if (incrementTestTime > addTestTime) {
            std::cout << incrementTestTime << " > " << addTestTime << '\n';
            std::cout << "Add beat increment by " << (incrementTestTime - addTestTime) << '\n';
            std::cout << '\n';
            addTestScore++;
        } else {
            std::cout << incrementTestTime << " = " << addTestTime << '\n';
        }
    }

    std::cout << "Increment test score: " << incrementTestScore << '\n';
    std::cout << "Add test score: " << addTestScore << '\n';

    return 0;
}

cpp-testing's People

Contributors

macintosh-fan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.