// ----------
// Arrays.c++
// ----------
#include <algorithm> // copy, equal
#include <cassert> // assert
#include <cstring> // strcmp
#include <iostream> // cout, endl
#include <stdexcept> // out_of_range
#include "Array1.h"
#include "Array2.h"
template <typename T, typename U>
void arrays (
const typename T::value_type& v1,
const typename T::value_type& v2,
const typename T::value_type& v3,
const typename U::value_type& w) {
typedef T array_type1;
typedef U array_type2;
{
const array_type1 x;
assert(x.size() == 3);
const int a[] = {0, 0, 0};
assert(std::equal(x.begin(), x.end(), a) == true);
}
{
const array_type1 x(v1);
assert(x.size() == 3);
const int a[] = {v1, v1, v1};
assert(std::equal(x.begin(), x.end(), a) == true);
}
{
const int a[] = {v1, v2, v3};
const int s = sizeof(a) / sizeof(a[0]);
const array_type1 x(a, a + s);
assert(std::equal(x.begin(), x.end(), a) == true);
}
{
const int a[] = {v1, v2, v3};
const int s = sizeof(a) / sizeof(a[0]);
const array_type1 x(a, a + s);
assert(x[1] == v2);
// assert(x[3] == v3);
try {
assert(x.at(3) == v3);}
catch (const std::out_of_range& e) {
assert(strcmp(e.what(), "Array::at index out of range") == 0);}
}
{
const array_type1 x(v1);
array_type1 y = x;
assert(x == y);
}
{
const array_type1 x(v1);
array_type1 y(v2);
y = x;
assert(x == y);
}
{
const array_type1 x(v1);
array_type2 y(w);
std::copy(x.begin(), x.end(), y.begin());
assert(std::equal(x.begin(), x.end(), y.begin()) == true);
}
{
const array_type1 x(v1);
const array_type1 y(v1);
assert(x == y);
assert(x <= y);
assert(x >= y);
assert(!(x != y));
assert(!(x < y));
assert(!(x > y));
}}
int main () {
using namespace std;
using namespace std::rel_ops;
cout << "Arrays.c++" << endl;
arrays< Array1<int, 3>, Array1<double, 5> >(2, 3, 4, 5.6);
arrays< Array2<int, 3>, Array2<double, 5> >(2, 3, 4, 5.6);
cout << "Done." << endl;
return 0;}
syntax highlighted by Code2HTML, v. 0.9.1