online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include "tests.h" int main() { RunTests(); return 0; }
#ifndef BUBBLE_SORT_H_ #define BUBBLE_SORT_H_ #include <utility> template <class T> void BubbleSort(T array[], const int size) { for (auto i = 0; i < size - 1; i++) { for (auto j = 1; j < size - i; j++) { if (array[j] < array[j - 1]) { std::swap(array[j - 1], array[j]); } } } } #endif // BUBBLE_SORT_H_
#ifndef TESTS_H_ #define TESTS_H_ void RunTests(); #endif // TESTS_H_
#include "tests.h" #include <string> #include <vector> #include "test_functions.h" #include "bubble_sort.h" template <class T> bool IsSorted(T array[], const int size) { for (auto i = 0; i < size - 1; i++) { if (array[i + 1] < array[i]) { return false; } } return true; } TEST(TestSortRandomInt) { int numbers[] = {5, 1, 89, 123, 34, 244, 26, 73, 4, 55}; BubbleSort(numbers, sizeof(numbers) / sizeof(int)); ASSERT(IsSorted(numbers, sizeof(numbers) / sizeof(int)), "Sort failed"); } TEST(TestSortReverseInt) { int numbers[] = {244, 123, 89, 73, 55, 34, 26, 5, 4, 1}; BubbleSort(numbers, sizeof(numbers) / sizeof(int)); ASSERT(IsSorted(numbers, sizeof(numbers) / sizeof(int)), "Sort failed"); } TEST(TestSortSortedInt) { int numbers[] = {1, 4, 5, 26, 34, 55, 73, 89, 123, 244}; BubbleSort(numbers, sizeof(numbers) / sizeof(int)); ASSERT(IsSorted(numbers, sizeof(numbers) / sizeof(int)), "Sort failed"); } TEST(TestSortRandomDouble) { double numbers[] = {5.3, 1.0, 89.45, 123.2, 34.87, 244.6, 26.5, 73.8, 4.54, 55.66}; BubbleSort(numbers, sizeof(numbers) / sizeof(double)); ASSERT(IsSorted(numbers, sizeof(numbers) / sizeof(double)), "Sort failed"); } TEST(TestSortReverseDouble) { double numbers[] = {244.6, 123.2, 89.45, 73.8, 55.66, 34.87, 26.5, 5.3, 4.54, 1.0}; BubbleSort(numbers, sizeof(numbers) / sizeof(double)); ASSERT(IsSorted(numbers, sizeof(numbers) / sizeof(double)), "Sort failed"); } TEST(TestSortSortedDouble) { double numbers[] = {1.0, 4.54, 5.3, 26.5, 34.87, 55.66, 73.8, 89.45, 123.2, 244.6}; BubbleSort(numbers, sizeof(numbers) / sizeof(double)); ASSERT(IsSorted(numbers, sizeof(numbers) / sizeof(double)), "Sort failed"); } const auto array_size = 10; TEST(TestSortRandomString) { std::string numbers[array_size] = {"fuenf", "eins", "neunundachtzig", "hundertdreiundzwanzig", "vierunddreissig", "zweihundertvierundvierzig", "sechsundzwanzig", "dreiundsiebzig", "vier", "fuenfundfuenfzig"}; BubbleSort(numbers, array_size); ASSERT(IsSorted(numbers, array_size), "Sort failed"); } TEST(TestSortReverseString) { std::string numbers[array_size] = {"zweihundertvierundvierzig", "vierunddreissig", "vier", "sechsundzwanzig", "neunundachtzig", "hundertdreiundzwanzig", "fuenfundfuenfzig", "fuenf", "eins", "dreiundsiebzig"}; BubbleSort(numbers, array_size); ASSERT(IsSorted(numbers, array_size), "Sort failed"); } TEST(TestSortSortedString) { std::string numbers[array_size] = {"dreiundsiebzig", "eins", "fuenf", "fuenfundfuenfzig", "hundertdreiundzwanzig", "neunundachtzig", "sechsundzwanzig", "vier", "vierunddreissig", "zweihundertvierundvierzig"}; BubbleSort(numbers, array_size); ASSERT(IsSorted(numbers, array_size), "Sort failed"); } TEST_SUITE(TestSort) { RUN_TEST(TestSortRandomInt); RUN_TEST(TestSortReverseInt); RUN_TEST(TestSortSortedInt); RUN_TEST(TestSortRandomDouble); RUN_TEST(TestSortReverseDouble); RUN_TEST(TestSortSortedDouble); RUN_TEST(TestSortRandomString); RUN_TEST(TestSortReverseString); RUN_TEST(TestSortSortedString); } void RunTests() { RUN_TEST_SUITE(TestSort); }
// Copyright 2018 MicroConsult GmbH #ifndef TEST_FUNCTIONS_H_ #define TEST_FUNCTIONS_H_ #include <stdio.h> static const int test_message_buffer_size = 1024; static char test_message_buffer[test_message_buffer_size]; #define TEST(fx) static void fx(const char** message) #define TEST_SUITE(fx) void fx(int* tests_total, int* tests_successful) #define RUN_TEST(test) \ do { \ const char* message = nullptr; \ ++*tests_total; \ test(&message); \ if (message) { \ printf("--- ERROR (%s): %s \n", #test, message); \ } \ else { \ ++*tests_successful; \ printf("+++ passed: %s\n", #test); \ } \ } while (0) #define RUN_TEST_SUITE(test_suite) \ do { \ int tests_total = 0; \ int tests_successful = 0; \ test_suite(&tests_total, &tests_successful); \ printf("-----------------------------------------------\n"); \ if (tests_successful == tests_total) { \ printf("+ Test Suite '%s': all %d tests passed\n\n", #test_suite, \ tests_total); \ } \ else { \ printf("- Test Suite '%s': %d of %d tests passed\n\n", #test_suite, \ tests_successful, tests_total); \ } \ } while (false) #define ASSERT(test, error_message) \ do { \ if (test) { \ *message = nullptr; \ } \ else { \ snprintf(test_message_buffer, test_message_buffer_size - 1, \ error_message); \ *message = test_message_buffer; \ return; \ } \ } while (false) #define ASSERT_INT(value, expected) \ do { \ if (value == expected) { \ *message = nullptr; \ } \ else { \ snprintf(test_message_buffer, test_message_buffer_size - 1, \ "Wrong value - expected: %d, current value: %d", expected, \ value); \ *message = test_message_buffer; \ return; \ } \ } while (false) #endif // TEST_FUNCTIONS_H_

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue