This is an experiment for Property Based Testing in CPP using the RapidCheck library
-
-
Save nunofernandes-plight/20b578a78661d7f048009243f0a6e953 to your computer and use it in GitHub Desktop.
Property Based Testing in C++ with RapidCheck
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Property Based Testing 20200111_1532_1.ipynb", | |
"provenance": [] | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "byHgyTEWbQcT", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"\n", | |
"\n", | |
"# Overview \n", | |
"\n", | |
"Testing [RapidCheck](https://github.com/emil-e/rapidcheck) as Property Based Testing Library for CPP \n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "OdwiMT-JbF-p", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"import os" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "OmCTFQ8nbS-H", | |
"colab_type": "code", | |
"outputId": "a466cf49-a71b-4e3f-b615-2ec15cb77945", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"source": [ | |
"!pwd" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"/content\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "YJCP-Cb3bX2M", | |
"colab_type": "code", | |
"outputId": "38263245-dc45-446f-8b81-80998c72162b", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"source": [ | |
"!ls" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"rapidcheck sample_data\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "jH6UdT6_bU1C", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"\n", | |
"# 1) Clone\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "JOb2HXclbT2Q", | |
"colab_type": "code", | |
"outputId": "8a9328bc-1c38-46a5-b9fb-ed207170eb51", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"source": [ | |
"!git clone https://github.com/emil-e/rapidcheck" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"fatal: destination path 'rapidcheck' already exists and is not an empty directory.\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "JofbSTv4bguC", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"\n", | |
"\n", | |
"# 2) Source \n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "JkSTgzlebaeF", | |
"colab_type": "code", | |
"outputId": "2ed43e86-d0b9-4f96-e53d-dea844e6c50b", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"source": [ | |
"%%writefile /content/main.cpp\n", | |
"\n", | |
"#include <rapidcheck.h>\n", | |
"\n", | |
"#include <string>\n", | |
"#include <vector>\n", | |
"#include <algorithm>\n", | |
"\n", | |
"using namespace std; \n", | |
"\n", | |
"template <typename T>\n", | |
"T sum(const T& a, const T& b) {return a+b;}\n", | |
"\n", | |
"template <typename T>\n", | |
"void test_sum()\n", | |
"{\n", | |
" rc::check(\"sum_1\", [](const T& a, const T& b) {RC_ASSERT(sum(a,b)==a+b);});\n", | |
" rc::check(\"sum_2\", [](const T& a, const T& b) {RC_ASSERT(sum(a,b)-a==b);});\n", | |
" rc::check(\"sum_3\", [](const T& a, const T& b) {RC_ASSERT(sum(a,b)-b==a);}); \n", | |
"}\n", | |
"\n", | |
"int main() {\n", | |
" rc::check(\"double reversal yields the original value\",\n", | |
" [](const std::vector<int> &l0) {\n", | |
" auto l1 = l0;\n", | |
" std::reverse(begin(l1), end(l1));\n", | |
" std::reverse(begin(l1), end(l1));\n", | |
" RC_ASSERT(l0 == l1);\n", | |
" });\n", | |
"\n", | |
"\n", | |
" cout << \"----------\" << endl; \n", | |
" cout << \"Testing Sum for int\" << endl; \n", | |
" test_sum<int>(); \n", | |
"\n", | |
" // This will fail because the float can not be tested with double equal but with negligible distance \n", | |
" /*\n", | |
" cout << \"----------\" << endl; \n", | |
" cout << \"Testing Sum for float\" << endl; \n", | |
" test_sum<float>(); \n", | |
" */\n", | |
"\n", | |
" cout << \"----------\" << endl; \n", | |
" cout << \"Testing Sum for string\" << endl; \n", | |
" rc::check(\"sum_str1\", [](const string a, const string b) {RC_ASSERT(sum(a,b)==a+b);});\n", | |
"\n", | |
" return 0;\n", | |
"}\n" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Overwriting /content/main.cpp\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ImHfUsrpbiBI", | |
"colab_type": "code", | |
"outputId": "2f27d9cc-eae4-46ac-94bd-e66ed9f48f1a", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"source": [ | |
"%%writefile /content/CMakeLists.txt\n", | |
"project (test)\n", | |
"add_subdirectory(\"/content/rapidcheck/\")\n", | |
"\n", | |
"add_executable(test\n", | |
" main.cpp\n", | |
")\n", | |
"\n", | |
"target_link_libraries(test rapidcheck)\n" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Writing /content/CMakeLists.txt\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "t_HoyXvtbk4i", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"\n", | |
"\n", | |
"# 3) Build\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "cB-MzjwQbjcP", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"os.chdir('/content/')" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "wO0VkMEHbmXq", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"!mkdir build" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "OwKdSVawboEi", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"os.chdir('/content/build')" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "HGzcuQDabpQV", | |
"colab_type": "code", | |
"outputId": "7b8bb716-5dba-40dc-8ff4-f8f219e8760c", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 470 | |
} | |
}, | |
"source": [ | |
"!cmake .." | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"-- The C compiler identification is GNU 7.4.0\n", | |
"-- The CXX compiler identification is GNU 7.4.0\n", | |
"-- Check for working C compiler: /usr/bin/cc\n", | |
"-- Check for working C compiler: /usr/bin/cc -- works\n", | |
"-- Detecting C compiler ABI info\n", | |
"-- Detecting C compiler ABI info - done\n", | |
"-- Detecting C compile features\n", | |
"-- Detecting C compile features - done\n", | |
"-- Check for working CXX compiler: /usr/bin/c++\n", | |
"-- Check for working CXX compiler: /usr/bin/c++ -- works\n", | |
"-- Detecting CXX compiler ABI info\n", | |
"-- Detecting CXX compiler ABI info - done\n", | |
"-- Detecting CXX compile features\n", | |
"-- Detecting CXX compile features - done\n", | |
"CMake Warning (dev) in CMakeLists.txt:\n", | |
" No cmake_minimum_required command is present. A line of code such as\n", | |
"\n", | |
" cmake_minimum_required(VERSION 3.12)\n", | |
"\n", | |
" should be added at the top of the file. The version specified may be lower\n", | |
" if you wish to support older CMake versions for this project. For more\n", | |
" information run \"cmake --help-policy CMP0000\".\n", | |
"This warning is for project developers. Use -Wno-dev to suppress it.\n", | |
"\n", | |
"-- Configuring done\n", | |
"-- Generating done\n", | |
"-- Build files have been written to: /content/build\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "p7wdDuonbqRG", | |
"colab_type": "code", | |
"outputId": "a326ccd7-fae1-41d4-bef9-563b04543ff8", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 101 | |
} | |
}, | |
"source": [ | |
"!make" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[ 94%] Built target rapidcheck\n", | |
"\u001b[35m\u001b[1mScanning dependencies of target test\u001b[0m\n", | |
"[ 97%] \u001b[32mBuilding CXX object CMakeFiles/test.dir/main.o\u001b[0m\n", | |
"[100%] \u001b[32m\u001b[1mLinking CXX executable test\u001b[0m\n", | |
"[100%] Built target test\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "D67AZtl6btPU", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"\n", | |
"# 4) Run \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "nxzgeDdkbrnt", | |
"colab_type": "code", | |
"outputId": "093bf9ec-10c8-4586-a0d1-084c5902be85", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 353 | |
} | |
}, | |
"source": [ | |
"!./test" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"Using configuration: seed=9954932559320983571\n", | |
"\n", | |
"- double reversal yields the original value\n", | |
"OK, passed 100 tests\n", | |
"----------\n", | |
"Testing Sum for int\n", | |
"\n", | |
"- sum_1\n", | |
"OK, passed 100 tests\n", | |
"\n", | |
"- sum_2\n", | |
"OK, passed 100 tests\n", | |
"\n", | |
"- sum_3\n", | |
"OK, passed 100 tests\n", | |
"----------\n", | |
"Testing Sum for string\n", | |
"\n", | |
"- sum_str1\n", | |
"OK, passed 100 tests\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "G9AG3pOybwV1", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment