@ -96,6 +96,101 @@ class StringLists {
// THE TEST CASES BEGIN HERE
class StringAppendOperatorTest { } ;
TEST ( StringAppendOperatorTest , IteratorTest ) {
DestroyDB ( kDbName , Options ( ) ) ; // Start this test with a fresh DB
StringAppendOperator append_op ( ' , ' ) ;
auto db_ = OpenDb ( & append_op ) ;
StringLists slists ( db_ ) ;
slists . Append ( " k1 " , " v1 " ) ;
slists . Append ( " k1 " , " v2 " ) ;
slists . Append ( " k1 " , " v3 " ) ;
slists . Append ( " k2 " , " a1 " ) ;
slists . Append ( " k2 " , " a2 " ) ;
slists . Append ( " k2 " , " a3 " ) ;
std : : string res ;
std : : unique_ptr < leveldb : : Iterator > it ( db_ - > NewIterator ( ReadOptions ( ) ) ) ;
std : : string k1 ( " k1 " ) ;
std : : string k2 ( " k2 " ) ;
bool first = true ;
for ( it - > Seek ( k1 ) ; it - > Valid ( ) ; it - > Next ( ) ) {
res = it - > value ( ) . ToString ( ) ;
if ( first ) {
ASSERT_EQ ( res , " v1,v2,v3 " ) ;
first = false ;
} else {
ASSERT_EQ ( res , " a1,a2,a3 " ) ;
}
}
slists . Append ( " k2 " , " a4 " ) ;
slists . Append ( " k1 " , " v4 " ) ;
// Snapshot should still be the same. Should ignore a4 and v4.
first = true ;
for ( it - > Seek ( k1 ) ; it - > Valid ( ) ; it - > Next ( ) ) {
res = it - > value ( ) . ToString ( ) ;
if ( first ) {
ASSERT_EQ ( res , " v1,v2,v3 " ) ;
first = false ;
} else {
ASSERT_EQ ( res , " a1,a2,a3 " ) ;
}
}
// Should release the snapshot and be aware of the new stuff now
it . reset ( db_ - > NewIterator ( ReadOptions ( ) ) ) ;
first = true ;
for ( it - > Seek ( k1 ) ; it - > Valid ( ) ; it - > Next ( ) ) {
res = it - > value ( ) . ToString ( ) ;
if ( first ) {
ASSERT_EQ ( res , " v1,v2,v3,v4 " ) ;
first = false ;
} else {
ASSERT_EQ ( res , " a1,a2,a3,a4 " ) ;
}
}
// start from k2 this time.
for ( it - > Seek ( k2 ) ; it - > Valid ( ) ; it - > Next ( ) ) {
res = it - > value ( ) . ToString ( ) ;
if ( first ) {
ASSERT_EQ ( res , " v1,v2,v3,v4 " ) ;
first = false ;
} else {
ASSERT_EQ ( res , " a1,a2,a3,a4 " ) ;
}
}
slists . Append ( " k3 " , " g1 " ) ;
it . reset ( db_ - > NewIterator ( ReadOptions ( ) ) ) ;
first = true ;
std : : string k3 ( " k3 " ) ;
for ( it - > Seek ( k2 ) ; it - > Valid ( ) ; it - > Next ( ) ) {
res = it - > value ( ) . ToString ( ) ;
if ( first ) {
ASSERT_EQ ( res , " a1,a2,a3,a4 " ) ;
first = false ;
} else {
ASSERT_EQ ( res , " g1 " ) ;
}
}
for ( it - > Seek ( k3 ) ; it - > Valid ( ) ; it - > Next ( ) ) {
res = it - > value ( ) . ToString ( ) ;
if ( first ) {
// should not be hit
ASSERT_EQ ( res , " a1,a2,a3,a4 " ) ;
first = false ;
} else {
ASSERT_EQ ( res , " g1 " ) ;
}
}
}
TEST ( StringAppendOperatorTest , SimpleTest ) {
DestroyDB ( kDbName , Options ( ) ) ; // Start this test with a fresh DB