#include #include #include #include #include "JTools/JArray.hh" #include "JTools/JMultiKey.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" /** * \file * * Example program to test JTOOLS::JArray class. * \author mdejong */ int main(int argc, char **argv) { using namespace std; using namespace JPP; int debug; try { JParser<> zap("Example program to test fixed length array class."); zap['d'] = make_field(debug) = 3; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } { //JArray<3, int> array(1, 2); // generates compiler error //JArray<3, int> array(1, 2, 3, 4); // generates compiler error //JArray<3, int> array(1, 2, "abc"); // generates compiler error } { const int i0 = 1; JArray<1, int> array(i0); ASSERT(i0 == array[0]); } { const int i0 = 1; const int i1 = 2; JArray<2, int> array(i0, i1); ASSERT(i0 == array[0]); ASSERT(i1 == array[1]); } { const int N = 9; typedef JArray JArray1_t; typedef JArray JArray2_t; JArray1_t array; for (JArray1_t::iterator i = array.begin(); i != array.end(); ++i) { *i = distance(array.begin(),i); } if (debug >= debug_t) { cout << "input A "; copy(array.begin(), array.end(), ostream_iterator(cout, " ")); cout << endl; } { JArray1_t buffer = array + array; for (int i = 0; i != N; ++i) { ASSERT(2*array[i] == buffer[i]); } } { JArray2_t buffer = array.pop_front(); if (debug >= debug_t) { cout << "pop_front "; copy(buffer.begin(), buffer.end(), ostream_iterator(cout, " ")); cout << endl; } for (int i = 0; i != N-1; ++i) { ASSERT(array[i+1] == buffer[i]); } } { JArray1_t buffer(array.pop_back(), array[N-1]); ASSERT(array == buffer); } { JArray2_t buffer = array.pop_back(); if (debug >= debug_t) { cout << "pop_back "; copy(buffer.begin(), buffer.end(), ostream_iterator(cout, " ")); cout << endl; } for (int i = 0; i != N-1; ++i) { ASSERT(array[i] == buffer[i]); } } } { JArray<3, int> array(1, 2, 3); JArray<3, const int> p3(array); if (debug >= debug_t) { copy(p3.begin(), p3.end(), ostream_iterator(cout, " ")); cout << endl; } ASSERT(array[0] == p3[0]); JArray<2, const int> p2(p3.pop_front()); if (debug >= debug_t) { copy(p2.begin(), p2.end(), ostream_iterator(cout, " ")); cout << endl; } ASSERT(array[1] == p2[0]); JArray<1, const int> p1(p2.pop_front()); if (debug >= debug_t) { copy(p1.begin(), p1.end(), ostream_iterator(cout, " ")); cout << endl; } ASSERT(array[2] == p1[0]); //JArray<0, const int> p0(p1.pop_front()); // compile error, as should be } { JMultiKey<2, int> key; key.first = 0; key.second.first = 1; int i = 2; JArray<3, int> array(key, i); if (debug >= debug_t) { copy(array.begin(), array.end(), ostream_iterator(cout, " ")); cout << endl; } ASSERT(array[0] == key.first); ASSERT(array[1] == key.second.first); ASSERT(array[2] == i); } return 0; }