diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..16e6d6330d2dfb4141b6f6b928f21dc835dd69dc --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +DEPS=combo.h cut.h Makefile tests.h +SRCS=combo.cpp cut.cpp main.cpp tests.cpp +BIN=main.bin +OBJS = $(SRCS:.cpp=.o) + +TEST_SRCS=unit_test.cpp combo.cpp cut.cpp tests.cpp +TEST_BIN=unit_test.bin + +TEST_OBJS = $(TEST_SRCS:.cpp=.o) + +CXXFLAGS = -g -Wall +TESTCXXFLAGS = -I${HOME}/include + +all : $(BIN) $(TEST_BIN) + +%.o: %.cpp $(DEPS) + $(CXX) -c -o $@ $< $(CXXFLAGS) $(TESTCXXFLAGS) + +$(BIN) : $(OBJS) + $(CXX) -o $@ $^ $(CXXFLAGS) + +$(TEST_BIN) : $(TEST_OBJS) + $(CXX) -o $@ $^ $(TESTCXXFLAGS) + ./$(TEST_BIN) + +clean : + rm -rf $(OBJS) $(BIN) $(TEST_OBJS) $(TEST_BIN) + diff --git a/codeseries.zip b/codeseries.zip new file mode 100644 index 0000000000000000000000000000000000000000..22c3d6ad1ef4fa8dff1099cb9c326a0fa323ba0e Binary files /dev/null and b/codeseries.zip differ diff --git a/combo.cpp b/combo.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f7cf691542fb1147933cd059fadd1967a777e78 --- /dev/null +++ b/combo.cpp @@ -0,0 +1,57 @@ +//Copyright (c) 2013 Bradley M. Small +#include "combo.h" + +Combo::Combo(Cut cut, int Count) : count(Count) , comb(Count,cut), rolled(false){} + +Combo::Combo(const Combo& other) : count(other.count), comb(other.comb), rolled(false){} + +Combo& Combo::operator++() { + int rollCount( 0 ); + for (std::vector<Cut>::reverse_iterator x = comb.rbegin(); x < comb.rend(); ++x){ + ++(*x); + if(!(*x).isRolled()) break; + ++rollCount; + } + if (rollCount == count) + rolled = true; + return *this; +} + +Combo& Combo::operator--() { + int rollCount( 0 ); + for (std::vector<Cut>::reverse_iterator x = comb.rbegin(); x < comb.rend(); ++x){ + --(*x); + if(!(*x).isRolled()) break; + ++rollCount; + } + if (rollCount == count) + rolled = true; + + return *this; +} + +Combo Combo::operator++(int) { + Combo c(*this); + ++(*this); + return c; +} + +Combo Combo::operator--(int) { + Combo c(*this); + --(*this); + return c; +} + +bool Combo::isRolled() { + return (rolled)?rolled=false, true:false; +} + +std::ostream& operator<<(std::ostream& stream, const Combo &c) { + + for (std::vector<Cut>::const_iterator x = c.comb.begin(); x < c.comb.end(); ++x){ + stream << (*x); + } + stream << std::endl; + return stream; +} + diff --git a/combo.h b/combo.h new file mode 100644 index 0000000000000000000000000000000000000000..e4112b62fc701578a931147dd642ec583c060608 --- /dev/null +++ b/combo.h @@ -0,0 +1,23 @@ +//Copyright (c) 2013 Bradley M. Small +#ifndef _COMBO_H_ +#define _COMBO_H_ +#include <vector> + +#include "cut.h" +class Combo { +public: + Combo(Cut cut = Cut('A', 'A', 'E'), int Count = 6); + Combo(const Combo & other); + Combo& operator++(); + Combo& operator--(); + Combo operator++(int); + Combo operator--(int); + friend class Tests; + friend std::ostream& operator<<(std::ostream& stream, const Combo& c); + bool isRolled(); +private: + int count; + std::vector<Cut> comb; + bool rolled; +}; +#endif diff --git a/cut.cpp b/cut.cpp new file mode 100644 index 0000000000000000000000000000000000000000..465ce7a58c071451381f4d34ccafc6be908288b2 --- /dev/null +++ b/cut.cpp @@ -0,0 +1,56 @@ +//Copyright (c) 2013 Bradley M. Small +#include <iostream> +#include "cut.h" + +Cut::Cut(char Val, char Low, char High) : low(Low), high(High), val(Val), rolled(false) { } + +Cut::Cut(const Cut& other) : low(other.low), high(other.high), val(other.val), rolled(false) { } + +Cut& Cut::operator++() { + if(++val > high) { + val = low; + rolled=true; + } + return *this; +} + +Cut& Cut::operator--() { + if(--val < low) { + val = high; + rolled=true; + } + return *this; +} + +Cut Cut::operator++(int) { + Cut result(*this); + ++(*this); + return result; +} + +Cut Cut::operator--(int) { + Cut result(*this); + --(*this); + return result; +} + +bool Cut::isRolled() { + return (rolled)?rolled=false, true:false; +} + +bool Cut::operator==(const Cut & rhs) const { + return (this->val == rhs.val); +} + +bool operator<(const Cut & lhs, const Cut & rhs) { + return (lhs.val < rhs.val); +} + +std::ostream& operator<<(std::ostream& stream, const Cut& cut) { + stream << cut.val; + return stream; +} + +int operator-(const Cut & lhs, const Cut & rhs) { + return (lhs.val - rhs.val); +} diff --git a/cut.h b/cut.h new file mode 100644 index 0000000000000000000000000000000000000000..bbe54e02dee79096022239d84b963ec38f859241 --- /dev/null +++ b/cut.h @@ -0,0 +1,27 @@ +//Copyright (c) 2013 Bradley M. Small +#ifndef _CUT_H_ +#define _CUT_H_ +#include<iostream> +class Cut { +public: + Cut(char Val='A', char Low='A', char High='Z'); + Cut(const Cut& other); + bool isRolled(); + Cut& operator++(); + Cut& operator--(); + Cut operator++(int); + Cut operator--(int); + bool operator==(const Cut & rhs) const ; + friend bool operator<(const Cut & lhs, const Cut & rhs); + friend int operator-(const Cut&, const Cut& ); + friend std::ostream& operator<<(std::ostream& stream, const Cut& cut); + char GetHigh() const {return high;}; + char GetLow() const {return low; }; + char GetVal() const {return val; }; +private: + char low; + char high; + char val; + bool rolled; +}; +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32ee1a08744f95b59e318f61c5e4a9bd199522a2 --- /dev/null +++ b/main.cpp @@ -0,0 +1,58 @@ +//Copyright (c) 2013 Bradley M. Small +#include <iostream> +#include "cut.h" +#include "combo.h" +#include "tests.h" +#include <algorithm> +#include <cmath> + +const bool debug ( false ); + +bool isValid(Combo combo) { + // apply rules here + bool retVal (true); + Tests t; + const size_t mostContiguous ( 3 ); + const size_t mostPresent ( 4 ); + const size_t maxAdjacent ( 2 ); + + if(t.MostContiguous(combo) > mostContiguous) { + if(debug) std::cout << "\tMaxContig:" << combo; + retVal = false; + } + if(t.MostPresent(combo) > mostPresent) { + if(debug) std::cout << "\tMaxPresent:" << combo; + retVal = false; + } + if(t.MaxAdjacent(combo) > maxAdjacent) { + if(debug) std::cout << "\tMaxAdjacent:" << combo; + retVal = false; + } + + return retVal; +} + +int main( void ) { + Cut cut('A', 'A', 'E'); + Combo combo(cut, 6); + + std::vector<Combo> codeSeries; + + for(;!combo.isRolled();){ + if( isValid(combo) ) + codeSeries.push_back(combo); + ++combo; + } + + std::random_shuffle(codeSeries.begin(), codeSeries.end()); + + int width = ceil(log10(codeSeries.size())); + int num(1); + + for (std::vector<Combo>::iterator x = codeSeries.begin(); x < codeSeries.end(); ++x) { + std::cout.width(width); + std::cout.fill('0'); + std::cout << num++ << " : " << *x; + } + return 0; +} diff --git a/tests.cpp b/tests.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8aa726646631ffcce0c18cb3ec593753c1398a02 --- /dev/null +++ b/tests.cpp @@ -0,0 +1,93 @@ +//Copyright (c) 2013 Bradley M. Small +#include "tests.h" +#include "combo.h" +#include "cut.h" +#include <vector> +#include <cstdlib> +#include <map> + +size_t Tests::MostContiguous( const Combo & combo ){ + size_t High (1), CurrentCount( 1 ); + + for (std::vector<Cut>::const_iterator x = combo.comb.begin() + 1; x < combo.comb.end(); ++x) { + if(*x == *(x-1)) { + ++CurrentCount; + if(CurrentCount > High) + High = CurrentCount; + } else { + CurrentCount = 1; + } + } + return (High); +} + +size_t Tests::LeastContiguous ( const Combo & combo ){ + size_t Low(combo.comb.size()), CurrentCount(1); + for (std::vector<Cut>::const_iterator x = combo.comb.begin() + 1; x < combo.comb.end(); ++x) { + if(*x == *(x-1)) { + ++CurrentCount; + } else { + if(CurrentCount < Low) + Low = CurrentCount; + CurrentCount = 1; + } + } + return (Low); +} + +size_t Tests::MostPresent ( const Combo & combo ){ + std::map<Cut,size_t> cutMap; + size_t Max ( 1 ); + for (std::vector<Cut>::const_iterator x = combo.comb.begin(); x < combo.comb.end(); ++x) { + if(cutMap.find(*x) == cutMap.end()) { + cutMap[*x] = 1; + } else { + ++cutMap[*x]; + } + } + for(std::map<Cut, size_t>::const_iterator x = cutMap.begin(); x != cutMap.end(); ++x) { + if(x->second > Max) + Max = x->second; + } + return (Max); +} + +size_t Tests::LeastPresent ( const Combo & combo ){ + std::map<Cut,size_t> cutMap; + size_t Min ( combo.comb.size() ); + for (std::vector<Cut>::const_iterator x = combo.comb.begin(); x < combo.comb.end(); ++x) { + if(cutMap.find(*x) == cutMap.end()) { + cutMap[*x] = 1; + } else { + ++cutMap[*x]; + } + } + for(std::map<Cut, size_t>::const_iterator x = cutMap.begin(); x != cutMap.end();++x) { + if(x->second < Min) + Min = x->second; + } + return(Min); +} + +size_t Tests::MaxAdjacent ( const Combo & combo ){ + size_t Max ( 0 ); + size_t CurrentDiff ( 0 ); + for (std::vector<Cut>::const_iterator x = combo.comb.begin() + 1; x < combo.comb.end(); ++x) { + + CurrentDiff = abs(*x - *(x-1)); + if (CurrentDiff > Max) + Max = CurrentDiff; + } + return (Max); +} + +size_t Tests::MinAdjacent ( const Combo & combo ){ + size_t Min ( 25 ); + size_t CurrentDiff ( 0 ); + for (std::vector<Cut>::const_iterator x = combo.comb.begin() + 1; x < combo.comb.end(); ++x) { + CurrentDiff = abs(*x - *(x-1)); + if (CurrentDiff < Min) + Min = CurrentDiff; + } + return (Min); +} diff --git a/tests.h b/tests.h new file mode 100644 index 0000000000000000000000000000000000000000..196858bcd51722c394a35d90822b21c430af731d --- /dev/null +++ b/tests.h @@ -0,0 +1,19 @@ +//Copyright (c) 2013 Bradley M. Small +#ifndef _TESTS_H_ +#define _TESTS_H_ + +#include "combo.h" +#include "cut.h" + +class Tests { +public: + size_t MostContiguous ( const Combo & combo ); + size_t LeastContiguous ( const Combo & combo ); + size_t MostPresent ( const Combo & combo ); + size_t LeastPresent ( const Combo & combo ); + size_t MaxAdjacent ( const Combo & combo ); + size_t MinAdjacent ( const Combo & combo ); +private: +}; + +#endif diff --git a/unit_test.cpp b/unit_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aac51c3fc724cf5bb5ee24afb5372b65e5b34e02 --- /dev/null +++ b/unit_test.cpp @@ -0,0 +1,52 @@ +#include "tests.h" +#include "combo.h" +#include "cut.h" +#define BOOST_TEST_MODULE CodeSeriesTests +#include <boost/test/included/unit_test.hpp> + +BOOST_AUTO_TEST_SUITE( test_class_cut ) +BOOST_AUTO_TEST_CASE( test_cut_ctor0 ) +{ + Cut a; + + BOOST_CHECK_EQUAL( a.GetHigh(), 'Z' ); + BOOST_CHECK_EQUAL( a.GetLow() , 'A' ); + BOOST_CHECK_EQUAL( a.GetVal() , 'A' ); +} +BOOST_AUTO_TEST_CASE( test_cut_ctor1 ) +{ + Cut b('Q', 'P', 'R'); + + BOOST_CHECK_EQUAL( b.GetHigh(), 'R' ); + BOOST_CHECK_EQUAL( b.GetLow() , 'P' ); + BOOST_CHECK_EQUAL( b.GetVal() , 'Q' ); +} +BOOST_AUTO_TEST_CASE( test_cut_ctor2 ) +{ + Cut c('J'); + + BOOST_CHECK_EQUAL( c.GetHigh(), 'Z' ); + BOOST_CHECK_EQUAL( c.GetLow() , 'A' ); + BOOST_CHECK_EQUAL( c.GetVal() , 'J' ); + +} +BOOST_AUTO_TEST_CASE( test_cut_plusminus ) +{ + Cut d; + + BOOST_CHECK_EQUAL( d.GetVal() , 'A' ); + d++; + BOOST_CHECK_EQUAL( d.GetVal() , 'B' ); + ++d; + BOOST_CHECK_EQUAL( d.GetVal() , 'C' ); + --d; + BOOST_CHECK_EQUAL( d.GetVal() , 'B' ); + d--; + BOOST_CHECK_EQUAL( d.GetVal() , 'A' ); + d--; + BOOST_CHECK_EQUAL( d.GetVal() , 'Z' ); + d++; + BOOST_CHECK_EQUAL( d.GetVal() , 'A' ); + +} +BOOST_AUTO_TEST_SUITE_END()