void BottleImpl::copyRange(const BottleImpl& alt, int first, int len) { if (len==0) { clear(); return; } const BottleImpl *src = &alt; BottleImpl tmp; if (&alt == this) { tmp.fromString(toString()); src = &tmp; } clear(); if (len==-1) { len = (int)src->size(); } int last = first + len - 1; int top = (int)src->size()-1; if (first<0) { first = 0; } if (last<0) { last = 0; } if (first>top) { first = top; } if (last>top) { last = top; } if (last>=0) { for (int i=first; i<=last; i++) { add(src->get(i).cloneStorable()); } } }
void testString() { report(0,"testing string representation..."); String target = "hello \"my\" \\friend"; BottleImpl bot; bot.addInt(5); bot.addString("hello \"my\" \\friend"); String txt = bot.toString(); const char *expect = "5 \"hello \\\"my\\\" \\\\friend\""; checkEqual(txt,expect,"string rep"); BottleImpl bot2; bot2.fromString(txt); checkEqual(2,(int)bot2.size(),"return from string rep"); }
bool read(ConnectionReader& reader) override { if (!reader.isValid()) { return false; } receives++; BottleImpl bot; bot.read(reader); if (expectation==std::string("")) { report(1,"got unexpected input"); return false; } checkEqual(bot.toString(),expectation,"received bottle"); return true; }
void testSize() { report(0,"testing sizes..."); BottleImpl bot; checkEqual(0,(int)bot.size(),"empty bottle"); bot.addInt(1); checkEqual(1,(int)bot.size(),"add int"); bot.addString("hello"); checkEqual(2,(int)bot.size(),"add string"); bot.clear(); checkEqual(0,(int)bot.size(),"clear"); }
void testStreaming() { report(0,"testing streaming (just text mode)..."); BottleImpl bot; bot.addInt(5); bot.addString("hello"); BufferedConnectionWriter bbw(true); bot.write(bbw); String s; StringInputStream sis; StreamConnectionReader sbr; s = bbw.toString(); sis.add(s); Route route; sbr.reset(sis,NULL,route,s.length(),true); BottleImpl bot2; bot2.read(sbr); checkEqual(bot2.toString(),bot.toString(),"to/from stream"); }
void testBinary() { report(0,"testing binary representation..."); BottleImpl bot; bot.addInt(5); bot.addString("hello"); checkEqual(bot.isInt(0),true,"type check"); checkEqual(bot.isString(1),true,"type check"); ManagedBytes store(bot.byteCount()); bot.toBytes(store.bytes()); BottleImpl bot2; bot2.fromBytes(store.bytes()); checkEqual((int)bot2.size(),2,"recovery binary, length"); checkEqual(bot2.isInt(0),bot.isInt(0),"recovery binary, integer"); checkEqual(bot2.isString(1),bot.isString(1),"recovery binary, integer"); BottleImpl bot3; bot3.fromString("[go] (10 20 30 40)"); ManagedBytes store2(bot3.byteCount()); bot3.toBytes(store2.bytes()); bot.fromBytes(store2.bytes()); checkEqual(bot.get(0).isVocab(),true,"type check"); checkEqual(bot.get(1).isList(),true,"type check"); Bottle bot4("0 1 2.2 3"); size_t hsize; const char *hbuf = bot4.toBinary(&hsize); Bottle bot5; bot5.fromBinary(hbuf,hsize); checkEqual(bot5.size(),4,"player bug"); }