C++ Sorting Practice Place

https://github.com/ot32em/cpp_sorting_validation

Description

Provide a testing zone with Fixtures, Validation(by std::sort), and Result/Debug Message.
Validation accepts two style of sorting. Pass by container and pass by STL style iterator

Note

  • Must define CaseT/CaseSetT first
  • Default definition (in usage.cpp)
    using CaseT = std::vector<int>
    using CaseSetT = std::vector<CaseT>
    

Usage

Complete usage example described in usage.cpp

Snippet from usage.cpp

// prepare fixtures
CaseSetT case_set;
case_set << CornerCases() << RandomCases(); 

// validate sorting
validate<ByContainer, 
    bubble, 
    quick
    // put any sorting functor passing by vector. traditional style
>(case_set);

validate<
    ByIterator, 
    bubble_iter
    // put any sorting functor passing by iterator. STL style
>(case_set);

Fixtures

Add predefinied fixtures (in usage.cpp)

CaseSetT your_set;
case_set << RandomCases() << CornerCases();

Make your fixtures providers

struct YourFixture
{
    CaseSetT get() const {
        ...
        return ...;
    }
}

Add fixtures

case_set << YourFixture();

Write a sorting

Write your sorting passed by vector

struct YourSorting
{
    const char* name() const { return "Very fast sorting"; } // Print out usage
    
    void sort(CaseT& vec) const
    {
        // do sorting
    }
};

Write your STL style sorting passed by a pair of iterator

struct STLStyleSorting
{
    const char* name() const { return "STL style Sorting Name 2"; } // Print out usage
    
    template<typename BeginIter, typename EndIter>
    void sort(BeginIter beginIter, EndIter endIter) const
    {
        // do sorting...
    }
};

Validation

Validate your sorting:

validate<
    ByContainer, 
    YourSorting,
    YourSecondSorting, ...
>(case_set);

Validate your STL style sorting

validate<
    ByIterator, 
    STLStyleSorting,
    STLStyleSecondSorting, ...
>(case_set);

Validation Source: std::sort

Print out message

Successful message Example

Very fast sorting passed.

Failure message Example:

"Very fast sorting" failed at XXth case
    src: { 4, 3, 2 ,1}
    actual: { 4, 1, 2, 3}
    expected: { 1, 2, 3, 4}
comments powered by Disqus