//inline void read(istream & in, const bool savesizeinbits=true); static JSVAL getbs(JSARGS args) { HandleScope scope; String::Utf8Value dbname(args[0]->ToString()); String::Utf8Value key(args[1]->ToString()); EWAHBoolArray<LENGTH>* bs = (EWAHBoolArray<LENGTH>*) External::Unwrap(args[2]); if (!bs) { return v8::ThrowException(v8::String::New("In getbs bitset is Null")); } std::string val; leveldb::Status s; leveldb::DB* db = getDb(gcontext->dbl, dbname,s); if (!db) { return v8::ThrowException(v8::String::New(s.ToString().c_str())); } s = db->Get(leveldb::ReadOptions(), *key, &val); if (!s.ok()) { //if not found we return Null otherwise we throw exception if (s.IsNotFound()) return scope.Close(Null()); else return v8::ThrowException(v8::String::New(s.ToString().c_str())); } //cout<< "getbs:" << val.size() << " " << val << endl; std::stringstream ss(val); bs->read(ss, /*const bool savesizeinbits=*/true); return scope.Close(Integer::New(1)); }
bool testJoergBukowski() { cout << "[testing JoergBukowski]" << endl; bool isOk(true); vector<uint32_t> positions; positions.push_back(0); positions.push_back(36778); positions.push_back(51863); positions.push_back(134946); positions.push_back(137330); positions.push_back(147726); positions.push_back(147990); positions.push_back(151884); positions.push_back(156404); positions.push_back(158486); positions.push_back(159622); positions.push_back(163159); positions.push_back(164599); string indexfile("testingewahboolarray.bin"); ::remove(indexfile.c_str()); EWAHBoolArray<uword> myarray; for (vector<uint32_t>::const_iterator i = positions.begin(); i != positions.end(); ++i) { myarray.set(*i); ofstream out(indexfile.c_str(), ios::out | ios::binary); myarray.write(out); out.close(); EWAHBoolArray<uword> recovered; ifstream in(indexfile.c_str(), ios::binary); recovered.read(in); in.close(); vector < size_t > vals; recovered.appendSetBits(vals); if (vals.size() != static_cast<size_t> (i - positions.begin() + 1)) { cout << "failed to recover right number" << endl; isOk = false; } if (!equal(vals.begin(), vals.end(), positions.begin())) { cout << "failed to recover" << endl; isOk = false; } vals.clear(); for (typename EWAHBoolArray<uword>::const_iterator j = recovered.begin(); j != recovered.end(); ++j) vals.push_back(static_cast<uint32_t> (*j)); if (vals.size() != static_cast<size_t> (i - positions.begin() + 1)) { cout << "failed to recover right number -- iterator" << endl; isOk = false; } if (!equal(vals.begin(), vals.end(), positions.begin())) { cout << "failed to recover -- iterator" << endl; isOk = false; } } if (isOk) ::remove(indexfile.c_str()); if (!isOk) cout << testfailed << endl; return isOk; }
bool testPhongTran2() { cout << "[testing PhongTran2]" << endl; bool isOk(true); uword iTotal = static_cast<uword> (1000); // when 1000 does not fit in uword, then it will be casted EWAHBoolArray<uword> myarray; for (uword x = static_cast<uword> (100); x < iTotal; x++) { myarray.addWord(x); } string indexfile("testingewahboolarray.bin"); ::remove(indexfile.c_str()); ofstream out(indexfile.c_str(), ios::out | ios::binary); myarray.write(out); out.close(); EWAHBoolArray<uword> lmyarray; ifstream in(indexfile.c_str(), ios::binary); lmyarray.read(in); in.close(); if (!(myarray == lmyarray)) { cout << "bad news, they are not equal" << endl; cout << "size in bits: " << myarray.sizeInBits() << " vs. " << lmyarray.sizeInBits() << endl; isOk = false; } EWAHBoolArrayIterator<uword> i = myarray.uncompress(); EWAHBoolArrayIterator<uword> j = lmyarray.uncompress(); while (i.hasNext()) { if (!j.hasNext()) { cout << "the two arrays don't have the same size?" << endl; isOk = false; break; } uword val = i.next(); uword val2 = j.next(); if (val != val2) { cout << "the two arrays differ " << endl; isOk = false; } } if (isOk) ::remove(indexfile.c_str()); if (!isOk) cout << testfailed << endl; return isOk; }
// unit test contributed by Phong Tran bool testPhongTran() { cout << "[testing PhongTran]" << endl; bool isOk(true); EWAHBoolArray<uint32_t> myarray; for (uint32_t x = 0; x < 10000; x++) { myarray.addWord(x); } string indexfile("testingewahboolarray.bin"); ::remove(indexfile.c_str()); ofstream out(indexfile.c_str(), ios::out | ios::binary); myarray.write(out); out.close(); EWAHBoolArray<uint32_t> lmyarray; ifstream in(indexfile.c_str(), ios::binary); lmyarray.read(in); in.close(); EWAHBoolArrayIterator<uint32_t> i = myarray.uncompress(); EWAHBoolArrayIterator<uint32_t> j = lmyarray.uncompress(); while (i.hasNext() or j.hasNext()) { if ((!j.hasNext()) or (!i.hasNext())) { cout << "the two arrays don't have the same size?" << endl; isOk = false; break; } uint32_t val = i.next(); uint32_t val2 = j.next(); if (val != val2) { cout << "the two arrays differ" << endl; isOk = false; break; } } if (isOk) ::remove(indexfile.c_str()); if (!isOk) cout << testfailed << endl; return isOk; }
bool testSerialization() { cout << "[testing Serialization] word size = " << sizeof(uword)<< endl; EWAHBoolArray<uword> bitmap; for(int i = 0; i < 1<<31; i= 2*i +3) { bitmap.set(static_cast<size_t>(i)); } stringstream ss; EWAHBoolArray<uword> lmyarray; for (int k = 0; k < 10; ++k) { bitmap.write(ss); lmyarray.read(ss); if (lmyarray != bitmap) { return false; } typename EWAHBoolArray<uword>::const_iterator i = bitmap.begin(); typename EWAHBoolArray<uword>::const_iterator j = lmyarray.begin(); for (; i != bitmap.end(); ++i, ++j) { if (*i != *j) return false; } } return true; }
bool testEWAHBoolArray() { cout << "[testing EWAHBoolArray]" << endl; bool isOk(true); EWAHBoolArray<uword> myarray; BoolArray<uword> ba(10 * sizeof(uword) * 8); uword zero = 0; uword notzero = static_cast<uword> (~zero); myarray.addWord(zero); ba.setWord(0, zero); myarray.addWord(zero); ba.setWord(1, zero); myarray.addWord(zero); ba.setWord(2, zero); uword specialval = 1UL + (1UL << 4) + (static_cast<uword> (1) << (sizeof(uword) * 8 - 1)); myarray.addWord(specialval); ba.setWord(3, specialval); myarray.addWord(notzero); ba.setWord(4, notzero); myarray.addWord(notzero); ba.setWord(5, notzero); myarray.addWord(notzero); ba.setWord(6, notzero); myarray.addWord(notzero); ba.setWord(7, notzero); myarray.addWord(specialval); ba.setWord(8, specialval); myarray.addWord(zero); ba.setWord(9, zero); if (myarray.sizeInBits() != 10 * sizeof(uword) * 8) { cout << "expected " << 10 * sizeof(uword) * 8 << " bits but found " << myarray.sizeInBits() << endl; isOk = false; } string indexfile("testingewahboolarray.bin"); ::remove(indexfile.c_str()); ofstream out(indexfile.c_str(), ios::out | ios::binary); myarray.write(out); out.close(); EWAHBoolArray<uword> lmyarray; ifstream in(indexfile.c_str(), ios::binary); lmyarray.read(in); in.close(); if (!(myarray == lmyarray)) { cout << "bad news, they are not equal" << endl; cout << "size in bits: " << myarray.sizeInBits() << " vs. " << lmyarray.sizeInBits() << endl; isOk = false; } EWAHBoolArrayIterator<uword> i = myarray.uncompress(); EWAHBoolArrayIterator<uword> j = lmyarray.uncompress(); uint32_t k = 0; while (i.hasNext()) { if (!j.hasNext()) { cout << "the two arrays don't have the same size?" << endl; isOk = false; break; } uword val = i.next(); uword val2 = j.next(); uword valref = ba.getWord(k++); if (val != valref) { cout << "the two arrays differ from uncompressed array at " << k << " " << val << " " << val2 << " " << valref << endl; isOk = false; } if (val != val2) { cout << "the two arrays differ at " << k << " " << val << " " << val2 << " " << valref << endl; isOk = false; } } if (isOk) ::remove(indexfile.c_str()); if (!isOk) cout << testfailed << endl; return isOk; }