further yrange tests

This commit is contained in:
Christian Zimmermann 2022-11-18 19:50:19 +01:00
parent 99eb72bb76
commit 31bebecb23
3 changed files with 72 additions and 17 deletions

View file

@ -54,37 +54,37 @@ namespace CNORXZ
template <class I, typename MetaType> template <class I, typename MetaType>
bool IndexInterface<I,MetaType>::operator==(const IndexInterface& in) const bool IndexInterface<I,MetaType>::operator==(const IndexInterface& in) const
{ {
return in.mPos == mPos and *in.range() == *range(); return in.lex() == lex() and *in.range() == *range();
} }
template <class I, typename MetaType> template <class I, typename MetaType>
bool IndexInterface<I,MetaType>::operator!=(const IndexInterface& in) const bool IndexInterface<I,MetaType>::operator!=(const IndexInterface& in) const
{ {
return in.mPos != mPos or *in.range() != *range(); return in.lex() != lex() or *in.range() != *range();
} }
template <class I, typename MetaType> template <class I, typename MetaType>
bool IndexInterface<I,MetaType>::operator<(const IndexInterface& in) const bool IndexInterface<I,MetaType>::operator<(const IndexInterface& in) const
{ {
return mPos < in.mPos; return lex() < in.lex();
} }
template <class I, typename MetaType> template <class I, typename MetaType>
bool IndexInterface<I,MetaType>::operator>(const IndexInterface& in) const bool IndexInterface<I,MetaType>::operator>(const IndexInterface& in) const
{ {
return mPos > in.mPos; return lex() > in.lex();
} }
template <class I, typename MetaType> template <class I, typename MetaType>
bool IndexInterface<I,MetaType>::operator<=(const IndexInterface& in) const bool IndexInterface<I,MetaType>::operator<=(const IndexInterface& in) const
{ {
return mPos <= in.mPos; return lex() <= in.lex();
} }
template <class I, typename MetaType> template <class I, typename MetaType>
bool IndexInterface<I,MetaType>::operator>=(const IndexInterface& in) const bool IndexInterface<I,MetaType>::operator>=(const IndexInterface& in) const
{ {
return mPos >= in.mPos; return lex() >= in.lex();
} }
template <class I, typename MetaType> template <class I, typename MetaType>

View file

@ -49,11 +49,13 @@ namespace CNORXZ
// is one less than the max position (=end) // is one less than the max position (=end)
if(i != 0 and idx->lex() == idx->lmax()-1){ if(i != 0 and idx->lex() == idx->lmax()-1){
IB::mPos -= mBlockSizes[i] * idx->pos(); IB::mPos -= mBlockSizes[i] * idx->pos();
mLex -= mLexBlockSizes[i] * idx->lex();
(*idx) = 0; (*idx) = 0;
up(i-1); up(i-1);
return; return;
} }
IB::mPos += mBlockSizes[i]; IB::mPos += mBlockSizes[i];
mLex += mLexBlockSizes[i];
++(*idx); ++(*idx);
} }
@ -63,10 +65,12 @@ namespace CNORXZ
if(i != 0 and idx->pos() == 0){ if(i != 0 and idx->pos() == 0){
(*idx) = idx->lmax()-1; (*idx) = idx->lmax()-1;
IB::mPos += mBlockSizes[i] * idx->pos(); IB::mPos += mBlockSizes[i] * idx->pos();
mLex += mLexBlockSizes[i] * idx->lex();
down(i-1); down(i-1);
return; return;
} }
IB::mPos += mBlockSizes[i]; IB::mPos -= mBlockSizes[i];
mLex -= mLexBlockSizes[i];
--(*idx); --(*idx);
} }
@ -139,11 +143,12 @@ namespace CNORXZ
YIndex& YIndex::operator=(SizeT lexpos) YIndex& YIndex::operator=(SizeT lexpos)
{ {
mLex = lexpos; if(lexpos >= lmax()){
if(lexpos == lmax()){ mLex = lmax();
IB::mPos = pmax(); IB::mPos = pmax();
return *this; return *this;
} }
mLex = lexpos;
IB::mPos = 0; IB::mPos = 0;
for(SizeT i = 0; i != mIs.size(); ++i){ for(SizeT i = 0; i != mIs.size(); ++i){
*mIs[i] = (lex() / mLexBlockSizes[i]) % mIs[i]->lmax(); *mIs[i] = (lex() / mLexBlockSizes[i]) % mIs[i]->lmax();
@ -191,13 +196,18 @@ namespace CNORXZ
YIndex& YIndex::operator+=(Int n) YIndex& YIndex::operator+=(Int n)
{ {
return *this = lex() + n; if(static_cast<Int>(lex()) + n < 0){
return *this = 0;
}
return *this = static_cast<Int>(lex()) + n;
} }
YIndex& YIndex::operator-=(Int n) YIndex& YIndex::operator-=(Int n)
{ {
if(static_cast<Int>(lex()) - n < 0){
return *this = lex() - n; return *this = 0;
}
return *this = static_cast<Int>(lex()) - n;
} }
SizeT YIndex::lex() const SizeT YIndex::lex() const
@ -336,18 +346,20 @@ namespace CNORXZ
String YRange::stringMeta(SizeT pos) const String YRange::stringMeta(SizeT pos) const
{ {
String out = "["; const String blim = "[";
const String elim = "]";
const String dlim = ",";
String out = elim;
for(auto rit = mRVec.end()-1;;--rit){ for(auto rit = mRVec.end()-1;;--rit){
const SizeT cursize = (*rit)->size(); const SizeT cursize = (*rit)->size();
const SizeT curpos = pos % cursize; const SizeT curpos = pos % cursize;
out += (*rit)->stringMeta(curpos); out = (*rit)->stringMeta(curpos) + out;
pos -= curpos;
pos /= cursize; pos /= cursize;
if(rit == mRVec.begin()){ if(rit == mRVec.begin()){
out += "]"; out = blim + out;
break; break;
} }
out += ","; out = dlim + out;
} }
return out; return out;
} }

View file

@ -171,6 +171,7 @@ namespace
auto endxi = mr->end(); auto endxi = mr->end();
for(auto xi = mr->begin(); xi != endxi; ++xi){ for(auto xi = mr->begin(); xi != endxi; ++xi){
EXPECT_EQ(xi.pos(), cnt); EXPECT_EQ(xi.pos(), cnt);
EXPECT_EQ(xi.lex(), cnt);
auto meta = mkm(cnt); auto meta = mkm(cnt);
EXPECT_TRUE(*xi == DType(meta)); EXPECT_TRUE(*xi == DType(meta));
EXPECT_EQ((*xi).str(), toString(meta)); EXPECT_EQ((*xi).str(), toString(meta));
@ -181,6 +182,7 @@ namespace
auto endxxi = mrx->end(); auto endxxi = mrx->end();
for(auto xxi = mrx->begin(); xxi != endxxi; ++xxi){ for(auto xxi = mrx->begin(); xxi != endxxi; ++xxi){
EXPECT_EQ(xxi.pos(), cnt); EXPECT_EQ(xxi.pos(), cnt);
EXPECT_EQ(xxi.lex(), cnt);
auto ci = std::get<0>(xxi.pack()); auto ci = std::get<0>(xxi.pack());
auto ui = std::get<1>(xxi.pack()); auto ui = std::get<1>(xxi.pack());
Tuple<SizeT,String> meta(*(*ci),*(*ui)); Tuple<SizeT,String> meta(*(*ci),*(*ui));
@ -218,10 +220,51 @@ namespace
EXPECT_EQ(xi.pos(), cnt); EXPECT_EQ(xi.pos(), cnt);
auto meta = mkm(cnt); auto meta = mkm(cnt);
EXPECT_TRUE(*xi == DType(meta)); EXPECT_TRUE(*xi == DType(meta));
EXPECT_EQ(yr->stringMeta(cnt), toString(meta));
EXPECT_EQ((*xi).str(), toString(meta)); EXPECT_EQ((*xi).str(), toString(meta));
++cnt; ++cnt;
} }
}
TEST_F(YR_Test, Index)
{
auto yrx = std::dynamic_pointer_cast<YRange>(yr);
auto yi = yrx->begin();
EXPECT_EQ(yi.pmax(), yr->size());
EXPECT_EQ(yi.lmax(), yr->size());
EXPECT_EQ(yi.range(), yr);
EXPECT_EQ(yi.range(), yrx);
EXPECT_EQ(yi.dim(), 2u);
const SizeT mmsize = mMeta.size();
auto mkm = [&](SizeT i) { return Vector<DType>({DType(i/mmsize),DType(mMeta[i % mmsize])}); };
for(SizeT i = 0; i != yr->size(); ++i){
auto a = yi + i;
EXPECT_EQ(a.lex(), i);
EXPECT_EQ(a.pos(), i);
auto mmi = DType(mkm(i));
EXPECT_TRUE(a.meta() == mmi);
EXPECT_TRUE(*a == mmi);
EXPECT_EQ(a.stringMeta(), toString(mmi));
for(SizeT j = 0; j != yr->size(); ++j){
const Int jj = static_cast<Int>(j) - static_cast<Int>(i);
auto b = a + jj;
auto mmj = DType(mkm(j));
EXPECT_EQ(b.lex(), j);
EXPECT_EQ(b.pos(), j);
EXPECT_TRUE(*b == mmj);
EXPECT_EQ(b.stringMeta(), toString(mmj));
}
}
yi += yi.lmax() + 10;
EXPECT_EQ(yi.lex(), yi.lmax());
EXPECT_EQ(yi.pos(), yi.pmax());
yi -= yi.lmax() + 20;
EXPECT_EQ(yi.lex(), 0);
EXPECT_EQ(yi.pos(), 0);
} }
// RCast_Test // RCast_Test
} }