#include "catch.hpp" #include using namespace ngcore; using namespace std; #include "meshing.hpp" template class ClsWithIndexType { size_t size; public: ClsWithIndexType(size_t asize) : size(asize) {} using index_type = TIND; size_t Size() const { return size; } }; template class ClsWithRange : public ClsWithIndexType { public: ClsWithRange(size_t size) : ClsWithIndexType(size) {} T_Range Range() const { return {1, 1+this->Size()}; } }; TEST_CASE("Array") { Array array; #ifdef DEBUG CHECK_THROWS_AS(array[0], RangeException); CHECK_THROWS_AS(array.DeleteLast(), RangeException); CHECK_THROWS_AS(array.Last(), RangeException); #endif // DEBUG Array a_initlst = { 1., 2., 3.}; CHECK(a_initlst[1] == 2.); CHECK(a_initlst.Size() == 3); FlatArray fa_a = a_initlst; CHECK(typeid(fa_a) == typeid(FlatArray)); CHECK(fa_a.Size() == 3); CHECK(fa_a.Last() == 3.); a_initlst.DeleteLast(); CHECK(a_initlst.Last() == 2.); CHECK(a_initlst.Size() == 2); #ifdef DEBUG CHECK_THROWS_AS(fa_a[5], RangeException); #endif // DEBUG Array b = Array(4); b = 2; int count = 0; for(auto val : b) { count++; CHECK(val == 2); } CHECK(count == 4); // range tests CHECK(typeid(array.Range()) == typeid(T_Range)); Array intarray; CHECK(typeid(intarray.Range()) == typeid(T_Range)); CHECK(typeid(Range(intarray)) == typeid(T_Range)); int i = 0; for(auto j : Range(b)) CHECK(j == i++); i = 0; for(auto j : b.Range()) CHECK(j == i++); // pointindex is still 1 based Array piarray(2); i = 1; for(auto j : Range(piarray)) CHECK(j == i++); i = 1; for(auto j : piarray.Range()) CHECK(j == i++); // a class can implement index_type and Size as well. ClsWithIndexType clsi(3); CHECK(typeid(Range(clsi)) == typeid(T_Range)); i = 0; for(auto j : Range(clsi)) CHECK(j == i++); // if the class has a Range function prefer that one ClsWithRange clsr(3); CHECK(typeid(Range(clsr)) == typeid(T_Range)); i=1; for(auto j : Range(clsr)) CHECK(j == i++); CHECK(typeid(Range(size_t(4))) == typeid(T_Range)); CHECK(typeid(Range(4)) == typeid(T_Range)); }