/*- * See the file LICENSE for redistribution information. * * Copyright (c) 2002, 2012 Oracle and/or its affiliates. All rights reserved. */ #include "db_cxx.h" #include #ifdef HAVE_STRING_H #include #endif using namespace std; void test1() { int numberOfKeysToWrite= 10000; Db db(0,DB_CXX_NO_EXCEPTIONS); db.set_pagesize(512); int err= db.open(0, "test1.db", 0, DB_BTREE, DB_CREATE, 0); { int i= 0; Dbt key(&i,sizeof(i)); Dbt data(&i,sizeof(i)); for(;iget(&multikey,&multidata,DB_NEXT|DB_MULTIPLE_KEY); if(err==0) { Dbt key, data; DbMultipleKeyDataIterator i(multidata); while(err==0 && i.next(key,data)) { int actualKey; int actualData; memmove(&actualKey, key.get_data(), sizeof(actualKey)); memmove(&actualData, data.get_data(), sizeof(actualData)); if(actualKey!=actualData) { std::cout << "Error: key/data mismatch. " << actualKey << "!=" << actualData << std::endl; err= -1; } else { check[actualKey]++; } numberOfKeysRead++; } } else if(err!=DB_NOTFOUND) std::cout << "Error: dbc->get: " << db_strerror(err) << std::endl; } if(numberOfKeysRead!=numberOfKeysToWrite) { std::cout << "Error: key count mismatch. " << numberOfKeysRead << "!=" << numberOfKeysToWrite << std::endl; } for(int n=0;nclose(); } db.close(0); } void test2() { int numberOfKeysToWrite= 10000; Db db(0,DB_CXX_NO_EXCEPTIONS); db.set_flags(DB_DUP); db.set_pagesize(512); int err= db.open(0, "test2.db", 0, DB_BTREE, DB_CREATE, 0); { int i= 0; int k= 0; Dbt key(&k,sizeof(k)); Dbt data(&i,sizeof(i)); for(;iget(&multikey,&multidata,DB_NEXT|DB_MULTIPLE); if(err==0) { Dbt data; DbMultipleDataIterator i(multidata); while(err==0 && i.next(data)) { int actualData; memmove(&actualData, data.get_data(), sizeof(actualData)); if(numberOfKeysRead!=actualData) { std::cout << "Error: key/data mismatch. " << numberOfKeysRead << "!=" << actualData << std::endl; err= -1; } numberOfKeysRead++; } } else if(err!=DB_NOTFOUND) std::cout << "Error: dbc->get: " << db_strerror(err) << std::endl; } if(numberOfKeysRead!=numberOfKeysToWrite) { std::cout << "Error: key count mismatch. " << numberOfKeysRead << "!=" << numberOfKeysToWrite << std::endl; } dbc->close(); } db.close(0); } void test3() { int numberOfKeysToWrite= 10000; Db db(0,DB_CXX_NO_EXCEPTIONS); db.set_pagesize(512); int err= db.open(0, "test3.db", 0, DB_RECNO, DB_CREATE, 0); { int i= 0; Dbt key; Dbt data(&i,sizeof(i)); for(;iget(&multikey,&multidata,DB_NEXT|DB_MULTIPLE_KEY); if(err==0) { u_int32_t recno= 0; Dbt data; DbMultipleRecnoDataIterator i(multidata); while(err==0 && i.next(recno,data)) { u_int32_t actualData; memmove(&actualData, data.get_data(), sizeof(actualData)); if(recno!=actualData+1) { std::cout << "Error: recno/data mismatch. " << recno << "!=" << actualData << "+1" << std::endl; err= -1; } numberOfKeysRead++; } } else if(err!=DB_NOTFOUND) std::cout << "Error: dbc->get: " << db_strerror(err) << std::endl; } if(numberOfKeysRead!=numberOfKeysToWrite) { std::cout << "Error: key count mismatch. " << numberOfKeysRead << "!=" << numberOfKeysToWrite << std::endl; } dbc->close(); } db.close(0); } int main() { test1(); test2(); test3(); return (0); }