/// basic test for line iteration
void RangeSetLineIteratorTest::testBasicIteration()
{
    CharTextDocument doc;
    doc.setText( QStringLiteral("a1|b2|c3|d4|e5|f6").replace("|","\n"));
    TextRangeSet ranges(&doc);
    ranges.addRange(0,0);   // line: 0
    ranges.addRange(1,1);   // line: 0
    ranges.addRange(2,7);   // line: 0-2
    ranges.addRange(12,15); // line: 4-5

    RangeSetLineIterator itr(&ranges);

    testTrue( itr.hasNext() );
    testEqual( itr.next(), 0 );
    testTrue( itr.hasNext() );
    testEqual( itr.next(), 1 );
    testTrue( itr.hasNext() );
    testEqual( itr.next(), 2 );
    testTrue( itr.hasNext() );
    testEqual( itr.next(), 4 );
    testTrue( itr.hasNext() );
    testEqual( itr.next(), 5 );
    testFalse( itr.hasNext() );
    testEqual( itr.next(), -1 );
}
void testEmpty()
{ 
    struct Set *set = constructSet();
    
    testTrue(empty(set) == true, "New set is empty");
    add(set, 123);

    testTrue(empty(set) == false, "Non empty set is not empty");

    SetClose(set);
}
void TextDocumentScopesTest::testStartsWith()
{
    TextScopeManager* sm = Edbee::instance()->scopeManager();
    TextScope* source = sm->refTextScope("aa.bb.cc.dd.ee");

    testTrue( source->startsWith( sm->refTextScope("aa.bb") ) );
    testTrue( source->startsWith( sm->refTextScope("aa.*") ) );
    testTrue( source->startsWith( sm->refTextScope("*.bb") ) );
    testTrue( source->startsWith( sm->refTextScope("*") ) );
    testFalse( source->startsWith( sm->refTextScope("bb") ) );
    testFalse( source->startsWith( sm->refTextScope("bb") ) );
}
示例#4
0
void testInterp(const string& input, const string& expected)
{
    Stack<Value> result;
    Stack<Env*> globals;
    bool ok = CompileModule(input, globals, result);
    testTrue(ok);
    Stack<Block*> block(result.as<CodeObject>()->block());
    ok = interp->exec(block, result);
    if (!ok)
        cerr << "Error: " << result.asObject()->as<Exception>()->message() << endl;
    testTrue(ok);
    testEqual(repr(result.get()), expected);
}
void testSize()
{
    struct Set *set = constructSet();

    testTrue(size(set) == 0,  "New set is size 0");

    add(set, 1);
    testTrue(size(set) == 1,  "New set add one is size 1");

    rm(set, 1);
    testTrue(size(set) == 0,  "Old set rm one is size 0");

    SetClose(set);
}
示例#6
0
local void
testAblog()
{
	initFile();
	ablogDebug = 0;

	String Boolean_imp = "import from Boolean";
	String C0_def = "C0: Category == with";
	String C1_def = "C1: Category == C0 with";
	
	String D0_def = "D0: C0 with == add";
	String D1_def = "D1: C1 with == add";

	StringList lines = listList(String)(5, Boolean_imp, C0_def, C1_def, D0_def, D1_def);

	AbSynList code = listCons(AbSyn)(stdtypes(), abqParseLines(lines));
	AbSyn absyn = abNewSequenceL(sposNone, code);
	
	abPutUse(absyn, AB_Use_NoValue);
	
	Stab file = stabFile();
	Stab stab = stabPushLevel(file, sposNone, STAB_LEVEL_LARGE);

	scopeBind(stab, absyn);
	typeInfer(stab, absyn);
	
	testTrue("Declare is sefo", abIsSefo(absyn));
	testIntEqual("Error Count", 0, comsgErrorCount());
	
	Syme C0 = uniqueMeaning(stab, "C0");
	Syme C1 = uniqueMeaning(stab, "C1");
	Syme D0 = uniqueMeaning(stab, "D0");
	Syme D1 = uniqueMeaning(stab, "D1");
	AbSyn sefo1 = has(abFrSyme(D1), abFrSyme(C1));
	AbSyn sefo0 = has(abFrSyme(D1), abFrSyme(C0));
	tiSefo(stab, sefo0);
	tiSefo(stab, sefo1);

	AbLogic cond0 = ablogFrSefo(sefo0);
	AbLogic cond1 = ablogFrSefo(sefo1);
	
	afprintf(dbOut, "Implies: %pAbLogic %pAbLogic %d\n", cond1, cond0, ablogImplies(cond1, cond0));
	afprintf(dbOut, "Implies: %pAbLogic %pAbLogic %d\n", cond0, cond1, ablogImplies(cond0, cond1));

	testTrue("00", ablogImplies(cond0, cond0));
	testTrue("10", ablogImplies(cond1, cond0));
	testFalse("01",ablogImplies(cond0, cond1));
	testTrue("11", ablogImplies(cond1, cond1));
}
示例#7
0
/// A special split-merge
/// Which means the second change was placed OVER the previous one on both side
/// (A) 1:1:
/// (B) 0:0:axb
/// (M) 0:2:ab
///
/// - mergeResult("1:1:", "0:2:axb"), "0:2:ab");
///  (text = a[x]b[y]e)
///
///
void TextChangeTest::testMerge7_splitMergeInvert()
{
    {
        TextChange a(1,1,"") ;
        TextChange* b = new TextChange(0,0,"axb");
        testTrue( a.giveAndMerge(0,b));
        testEqual( a.offset(),0 );
        testEqual( a.docLength(),0 );
        testEqual( a.storedText(), "ab" );
    }

    {
        TextChange a(0,1,"") ;
        TextChange* b = new TextChange(0,0,"xab");
        testTrue( a.giveAndMerge(0,b));
        testEqual( a.offset(),0 );
        testEqual( a.docLength(),0 );
        testEqual( a.storedText(), "ab" );
    }


    {
        TextChange a(0,2,"") ;                                       // |abcd => [xy]abcd             (0:2:)
        TextChange* b = new TextChange(1,0,"yab");             // x[yab]cd  => xcd              (1:0:yab)
        testTrue( a.giveAndMerge(0,b));                                    // =>                            (0:1:ab)
        testEqual( a.offset(),0 );
        testEqual( a.docLength(),1 );
        testEqual( a.storedText(), "ab" );
    }

    {
        TextChange a(1,3,"bc") ;                                     // a[bc]def     => aXYZdef        (1:3:bc)
        TextChange* b = new TextChange(4,1,"de");              // aXYZ[de]f    => aXYZ?f         (4:1:de)
        testTrue( a.giveAndMerge(0,b));                                    // =>                             (1:4:bcde)
        testEqual( a.offset(),1 );
        testEqual( a.docLength(),4 );
        testEqual( a.storedText(), "bcde" );
    }

    {
        TextChange a(1,4,"bc") ;                                     // a[bc]def     => aKLMNdef       (1:4:bc)
        TextChange* b = new TextChange(2,2,"L");               // aK[L]MNdef  => aKxyMNdef       (2:2:L)
        testTrue( a.giveAndMerge(0,b));                                    // =>                             (1:5:bc)
        testEqual( a.offset(),1 );
        testEqual( a.docLength(),5 );
        testEqual( a.storedText(), "bc" );
    }

}
示例#8
0
文件: test_syme.c 项目: pdo/aldor
local void
testSymeSExpr()
{

	String aSimpleDomain = "+++Comment\nDom: Category == with {f: () -> () ++ f\n}";
	StringList lines = listList(String)(1, aSimpleDomain);
	AbSynList code = listCons(AbSyn)(stdtypes(), abqParseLines(lines));
	
	AbSyn absyn = abNewSequenceL(sposNone, code);

	initFile();
	Stab stab = stabFile();
	
	abPutUse(absyn, AB_Use_NoValue);
	scopeBind(stab, absyn);
	typeInfer(stab, absyn);

	testTrue("Declare is sefo", abIsSefo(absyn));
	testIntEqual("Error Count", 0, comsgErrorCount());

	SymeList symes = stabGetMeanings(stab, ablogFalse(), symInternConst("Dom"));
	testIntEqual("unique meaning", 1, listLength(Syme)(symes));

	Syme syme = car(symes);
	SExpr sx = symeSExprAList(syme);
	
	finiFile();
}
示例#9
0
int test_skip_truncated_varint() {
  // error expected if varint unfinished
  int unpack_result = types__iter_unpack(TRUNCATED_VARINT, sizeof(TRUNCATED_VARINT), iter_msg_buffer, sizeof(iter_msg_buffer));
  testTrue(unpack_result < 0, "error if reading truncated varint");

  return 1;
}
示例#10
0
int test_validation() {
  int unpack_result = 0;
  RequiredField2* c_req = required_field2__unpack(NULL, sizeof(F1V10), F1V10);
  unpack_result = required_field2__iter_unpack(F1V10, sizeof(F1V10), iter_msg_buffer, sizeof(iter_msg_buffer));
  testTrue((c_req != NULL) && (unpack_result >= 0),"don't validate required");

  StringField* c_string = string_field__unpack(NULL, sizeof(ZERO_BYTE_STRING), ZERO_BYTE_STRING);
  StringField* f_string = (StringField*)iter_msg_buffer;
  unpack_result = string_field__iter_unpack(ZERO_BYTE_STRING,  sizeof(ZERO_BYTE_STRING),  iter_msg_buffer, sizeof(iter_msg_buffer));
  testTrue((f_string->f1 != NULL) && (strcmp(c_string->f1,f_string->f1)==0),"don't validate string");

  unpack_result = types__iter_unpack(NEGATIVE_LENGTH_STRING,  sizeof(NEGATIVE_LENGTH_STRING),  iter_msg_buffer, sizeof(iter_msg_buffer));

  testTrue(unpack_result == PROTOBUF_ITER_WRONG_MESSAGE, "string length overflow");

  return 1;
}
示例#11
0
/// A special split-merge operation
/// Which means the second change was placed OVER the previous one on both side
/// (A) 1:0:BC
/// (B) 0:0:AD
/// (M) 0:0:ABCD
void TextChangeTest::testMerge6_splitMerge()
{
    TextChange a(1,0,"bc");
    TextChange* b = new TextChange(0,0,"ad");
    testTrue( a.giveAndMerge(0,b) );
    testEqual( a.offset(), 0 );
    testEqual( a.docLength(), 0 );
    testEqual( a.storedText(), "abcd" );
}
示例#12
0
/// Test the overlapped by function
void TextChangeTest::testBoundaryMethods()
{
    // INSERT TESTS
    TextChange s1(4,4,"");    //      [    ]
    TextChange s2(0,2,"");    // [  ]
    TextChange s3(5,2,"");    //       [  ]
    TextChange s4(1,2,"");    //  [  ]
    TextChange s5(2,2,"");    //    [  ]

    // test non overlap touch
    testFalse( s1.isOverlappedBy(&s2) );
    testFalse( s1.isTouchedBy(&s2) );
    testFalse( s2.isOverlappedBy(&s1) );
    testFalse( s2.isTouchedBy(&s1) );

    // test overlap (fully included)
    testTrue( s1.isOverlappedBy(&s3) );
    testFalse( s1.isTouchedBy(&s3) );
    testTrue( s3.isOverlappedBy(&s1) );
    testFalse( s3.isTouchedBy(&s1) );

    // test overlap not fully included
    testTrue( s4.isOverlappedBy(&s5));
    testFalse( s4.isTouchedBy(&s5));
    testTrue( s5.isOverlappedBy(&s4));
    testFalse( s5.isTouchedBy(&s5));

    // test touching items
    testTrue( s2.isTouchedBy(&s5));
    testFalse( s2.isOverlappedBy(&s5));
    testTrue( s5.isTouchedBy(&s2));
    testFalse( s5.isOverlappedBy(&s2));

    // BACKSPACE TEST
    TextChange d1(0,0,"Y");
    TextChange d2(1,0,"X");
    testFalse( d1.isTouchedBy(&d2) );
    testFalse( d1.isOverlappedBy(&d2) );
    testTrue( d2.isTouchedBy(&d1) );
    testFalse( d2.isOverlappedBy(&d1) );


    TextChange d3(0,0,"X");
    TextChange d4(0,0,"Y");
    testTrue( d3.isTouchedBy(&d4) );
    testFalse( d3.isOverlappedBy(&d4) );
    testTrue( d4.isTouchedBy(&d3) );
    testFalse( d4.isOverlappedBy(&d3) );
}
示例#13
0
local void
testTests()
{
	Foam foam = foamNewSet(foamNewLoc(1), foamNewLoc(1));
	testFalse("", foamIsMultiAssign(foam));

	foam = foamNewSet(foamNew(FOAM_Values, 2, foamNewLoc(1), foamNewLoc(2)),
			  foamNewLoc(3));
	testTrue("", foamIsMultiAssign(foam));
}
示例#14
0
/// MERGE 5
/// - a[b]cdefgh     =(wxyz)=> awxyzcdefgh   (A) 1:4:b
/// - awx[y]zcdefgh  =(q)=>    awxqzcdefgh   (B) 3:1:y
/// = This should be merge into:             (M) 1:4:b     =>  b
void TextChangeTest::testMerge5()
{
    TextChange a(1,4,"b");
    TextChange* b = new TextChange(3,1,"y");

    testTrue( a.giveAndMerge(0,b) );
    testEqual( a.offset(), 1 );
    testEqual( a.docLength(), 4 );
    testEqual( a.storedText(), "b" );
}
示例#15
0
/// MERGE 2
/// - a[bc]defgh   =(xyz)=> axyzdefgh (A) 1:3:bc
/// - [axyz]defgh  =(q)=>   qdefgh    (B) 0:1:axyz
/// = This should be merge into:      (M) 0:1:abc     =>  a + bc
void TextChangeTest::testMerge2()
{
    TextChange a(1,3,"bc");
    TextChange* b = new TextChange(0,1,"axyz");

    testTrue( a.giveAndMerge(0,b) );
    testEqual( a.offset(), 0 );
    testEqual( a.docLength(), 1 );
    testEqual( a.storedText(), "abc" );
}
示例#16
0
/// MERGE 1
/// - a[bc]defgh  =(x)=> axdefgh   (A) 1:1:bc
/// - a[xd]efgh   =(y)=> ayefgh    (B) 1:1:xd
/// = This should be merge into:   (M) 1:1:bcd        =>  bc + d
void TextChangeTest::testMerge1()
{
    TextChange a(1,1,"bc");
    TextChange* b = new TextChange(1,1,"xd");

    testTrue( a.giveAndMerge(0,b) );
    testEqual( a.offset(), 1 );
    testEqual( a.docLength(), 1 );
    testEqual( a.storedText(), "bcd" );
}
示例#17
0
void testReplacement(const string& input,
                     const string& expected,
                     InstrCode initial,
                     InstrCode replacement,
                     InstrCode next1 = InstrCodeCount,
                     InstrCode next2 = InstrCodeCount)
{
    Stack<Value> result;
    Stack<Env*> globals;
    bool ok = CompileModule(input, globals, result);
    testTrue(ok);
    Stack<Block*> block(result.as<CodeObject>()->block());

    InstrThunk* instrp = block->findInstr(Instr_Lambda);
    assert(instrp);
    Instr* instr = instrp->data;
    assert(instr->code() == Instr_Lambda);
    LambdaInstr* lambda = static_cast<LambdaInstr*>(instr);
    instrp = lambda->block()->findInstr(initial);
    assert(instrp);

    ok = interp->exec(block, result);
    if (!ok)
        cerr << "Error: " << result.asObject()->as<Exception>()->message() << endl;
    testTrue(ok);
    testEqual(repr(result.get()), expected);

    instr = instrp->data;
    testEqual(instrName(instr->code()), instrName(replacement));

    if (next1 < InstrCodeCount) {
        instr = getNextInstr(instr);
        assert(instr);
        testEqual(instrName(instr->code()), instrName(next1));

        if (next2 < InstrCodeCount) {
            instr = getNextInstr(instr);
            assert(instr);
            testEqual(instrName(instr->code()), instrName(next2));
        }
    }
}
示例#18
0
int test_openx() {
  BidRequest* c = bid_request__unpack(NULL, sizeof(OPENX_1), OPENX_1);
  BidRequest* f = (BidRequest*)iter_msg_buffer;
  bid_request__iter_unpack(OPENX_1, sizeof(OPENX_1), iter_msg_buffer, sizeof(iter_msg_buffer));

  testTrue(c->matching_ad_ids[0]->campaign_id==f->matching_ad_ids[0]->campaign_id, "OpenX equal Matching Ad Id");

  bid_request__free_unpacked(c, NULL);

  return 1;
}
示例#19
0
local void
testCall()
{
	Foam foam;

	foam = foamNew(FOAM_OCall, 3, FOAM_Clos, foamNewNil(), foamNewNil());
	testIntEqual("foamOCallArgc", 0, foamOCallArgc(foam));

	foam = foamNew(FOAM_OCall, 4, FOAM_Clos, foamNewNil(), foamNewNil(), foamNewSInt(1));
	testIntEqual("foamOCallArgc", 1, foamOCallArgc(foam));

	foam = foamNewPCall(FOAM_Proto_C, FOAM_NOp, foamNewGlo(int0), NULL);
	testIntEqual("argc", 0, foamPCallArgc(foam));
	testIntEqual("protocol", FOAM_Proto_C, foam->foamPCall.protocol);

	foam = foamNewCCall(FOAM_Word, foamNewGlo(int0), NULL);
	testIntEqual("argc", 0, foamCCallArgc(foam));
	testTrue("op", foamEqual(foamNewGlo(int0), foam->foamCCall.op));

	foam = foamNewCCall(FOAM_Word, foamNewGlo(int0), foamNewPar(1), NULL);
	testIntEqual("argc", 1, foamCCallArgc(foam));
	testTrue("op", foamEqual(foamNewPar(1), foam->foamCCall.argv[0]));
}
示例#20
0
void testException(const string& input, const string& expected)
{
    Stack<Value> result;
    Stack<Env*> globals;
    bool ok = CompileModule(input, globals, result);
    testTrue(ok);
    Stack<Block*> block(result.as<CodeObject>()->block());
    testExpectingException = true;
    ok = interp->exec(block, result);
    testExpectingException = false;
    if (ok) {
        cerr << "Expected exception but got: " << result << endl;
        abortTests();
    }
    Object *o = result.toObject();
    testTrue(o->isInstanceOf(Exception::ObjectClass));
    string message = o->as<Exception>()->fullMessage();
    if (message.find(expected) == string::npos) {
        cerr << "Expected message containing: " << expected << endl;
        cerr << "But got: " << message << endl;
        abortTests();
    }
}
示例#21
0
void testFullAndGrow()
{
    struct Set *set = constructSet();
    int i = 0;

    for (i = 0; i < STARTING_SET_SIZE * 3; i++) {
        add(set, i);
    }

    printf("%i %i %i\n", full(set), set->capacity, set->count);
    
    testTrue(set->count == 30, "added 30 elements");
    
    //rm(set, 29);
    for (i = 0; i < STARTING_SET_SIZE * 3; i++) {
        rm(set, i);
    }
    printf("%i %i %i\n", full(set), set->capacity, set->count);
    
    testTrue(set->count == 0, "removed 30 elements");
    
    SetClose(set);
}
/// This method tests the score selector
void TextDocumentScopesTest::testScopeSelectorRanking()
{
    TextScopeManager* sm = Edbee::instance()->scopeManager();
    sm->reset();

    TextScopeList* multiScope = sm->createTextScopeList("text.html.markdown meta.paragraph.markdown markup.bold.markdown");

    QList<TextScopeSelector*> selectors;
    selectors.append( new TextScopeSelector("text.* markup.bold") );
    selectors.append( new TextScopeSelector("text markup.bold") );
    selectors.append( new TextScopeSelector("markup.bold") );
    selectors.append( new TextScopeSelector("text.html meta.*.markdown markup") );
    selectors.append( new TextScopeSelector("text.html meta.* markup") );
    selectors.append( new TextScopeSelector("text.html * markup") );
    selectors.append( new TextScopeSelector("text.html markup") );
    selectors.append( new TextScopeSelector("text markup") );
    selectors.append( new TextScopeSelector("markup") );
    selectors.append( new TextScopeSelector("text.html") );
    selectors.append( new TextScopeSelector("text") );

    double lastRank = 1.0;
    for(int i = 0; i < selectors.size(); ++i)
    {
        TextScopeSelector* sel = selectors.at(i);
        double rank = sel->calculateMatchScore( multiScope );
        if( !(rank < lastRank ) ) {
            qlog_info() << "SCOPES: " << multiScope->toString();
            qlog_info() << "  PREV: " << selectors.at(i-1)->toString();
            qlog_info() << "  rank: " << lastRank;
            qlog_info() << "";
            qlog_info() << "   NEW: " << sel->toString();
            qlog_info() << "  rank: " << rank;
        }

        testTrue( rank < lastRank );
        lastRank = rank;
    }
    qDeleteAll(selectors);
    delete multiScope;
}
示例#23
0
void forgTest()
{
	Buffer buf = bufNew();

	ForeignOrigin forg1 = forgNew(FOAM_Proto_Java, strCopy("hello"));
	ForeignOrigin forg2 = forgNew(FOAM_Proto_C, strCopy("world"));
	ForeignOrigin forg3 = forgNew(FOAM_Proto_C, NULL);
	ForeignOrigin forg1_r, forg2_r, forg3_r;
	testTrue("1", forgEqual(forg1, forg1));
	testTrue("2", forgEqual(forg2, forg2));
	testFalse("3", forgEqual(forg1, forg2));
	testFalse("4", forgEqual(forg1, forg3));

	forgToBuffer(buf, forg1);
	forgToBuffer(buf, forg2);
	bufSetPosition(buf, 0);

	forg1_r = forgFrBuffer(buf);
	forg2_r = forgFrBuffer(buf);

	testTrue("5", forgEqual(forg1_r, forg1));
	testTrue("6", forgEqual(forg2_r, forg2));

	int pos = bufPosition(buf);
	bufSetPosition(buf, 0);
	forgBufferSkip(buf);
	forgBufferSkip(buf);
	testIntEqual("7", pos, bufPosition(buf));

	bufSetPosition(buf, 0);
	forgToBuffer(buf, forg3);
	forgToBuffer(buf, forg1);
	pos = bufPosition(buf);

	bufSetPosition(buf, 0);
	forg3_r = forgFrBuffer(buf);
	forg1_r = forgFrBuffer(buf);

	testTrue("8", forgEqual(forg3_r, forg3));
	testTrue("8", forgEqual(forg1_r, forg1));
	testIntEqual("10", pos, bufPosition(buf));
}
示例#24
0
int test_types() {
  Types _types_msg;
  Types* types_msg = &_types_msg;

  Types *types_iter_msg = (Types*)iter_msg_buffer;
  uint8_t pack_buffer[50*1024];
  int packed_size;
  int unpack_result;

  // double
  types_clean_msg();
  types_msg->t_double = 134148.15123;
  types_msg->has_t_double = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_double==types_msg->t_double, "double positive value");

  types_clean_msg();
  types_msg->t_double = -64322758.12234;
  types_msg->has_t_double = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_double==types_msg->t_double, "double negative value");

  // float
  types_clean_msg();
  types_msg->t_float = 48.15;
  types_msg->has_t_float = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_float==types_msg->t_float, "float positive value");

  types_clean_msg();
  types_msg->t_float = -2758.12;
  types_msg->has_t_float = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_float==types_msg->t_float, "float negative value");

  // int32
  types_clean_msg();
  types_msg->t_int32 = 14142;
  types_msg->has_t_int32 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_int32==types_msg->t_int32, "int32 positive value");

  types_clean_msg();
  types_msg->t_int32 = -14143;
  types_msg->has_t_int32 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_int32==types_msg->t_int32, "int32 negative value");

  // int64
  types_clean_msg();
  types_msg->t_int64 = 4295067796;
  types_msg->has_t_int64 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_int64==types_msg->t_int64, "int64 positive value");

  types_clean_msg();
  types_msg->t_int64 = -4295067796;
  types_msg->has_t_int64 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_int64==types_msg->t_int64, "int64 negative value");

  // uint32
  types_clean_msg();
  types_msg->t_uint32 = 14142;
  types_msg->has_t_uint32 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_uint32==types_msg->t_uint32, "uint32 value");

  // uint64
  types_clean_msg();
  types_msg->t_uint64 = 4295067796;
  types_msg->has_t_uint64 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_uint64==types_msg->t_uint64, "uint64 value");

  // sint32
  types_clean_msg();
  types_msg->t_sint32 = 14142;
  types_msg->has_t_sint32 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_sint32==types_msg->t_sint32, "sint32 positive value");

  types_clean_msg();
  types_msg->t_sint32 = -14143;
  types_msg->has_t_sint32 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_sint32==types_msg->t_sint32, "sint32 negative value");

  // sint64
  types_clean_msg();
  types_msg->t_sint64 = 4295067796;
  types_msg->has_t_sint64 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_sint64==types_msg->t_sint64, "sint64 positive value");

  types_clean_msg();
  types_msg->t_sint64 = -4295067796;
  types_msg->has_t_sint64 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_sint64==types_msg->t_sint64, "sint64 negative value");

  // fixed32
  types_clean_msg();
  types_msg->t_fixed32 = 14142;
  types_msg->has_t_fixed32 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_fixed32==types_msg->t_fixed32, "fixed32 value");

  // fixed64
  types_clean_msg();
  types_msg->t_fixed64 = 4295067796;
  types_msg->has_t_fixed64 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_fixed64==types_msg->t_fixed64, "fixed64 value");

  // sfixed32
  types_clean_msg();
  types_msg->t_sfixed32 = 14142;
  types_msg->has_t_sfixed32 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_sfixed32==types_msg->t_sfixed32, "sfixed32 positive value");

  types_clean_msg();
  types_msg->t_sfixed32 = -14142;
  types_msg->has_t_sfixed32 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_sfixed32==types_msg->t_sfixed32, "sfixed32 negative value");

  // sfixed64
  types_clean_msg();
  types_msg->t_sfixed64 = 4295067796;
  types_msg->has_t_sfixed64 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_sfixed64==types_msg->t_sfixed64, "sfixed64 positive value");

  types_clean_msg();
  types_msg->t_sfixed64 = -4295067796;
  types_msg->has_t_sfixed64 = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_sfixed64==types_msg->t_sfixed64, "sfixed64 negative value");

  // bool
  types_clean_msg();
  types_msg->t_bool = 1;
  types_msg->has_t_bool = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_bool==types_msg->t_bool, "bool true");

  types_clean_msg();
  types_msg->t_bool = 0;
  types_msg->has_t_bool = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->t_bool==types_msg->t_bool, "bool false");

  // enum
  types_clean_msg();
  types_msg->t_enum = TYPES__CHOICES__c1;
  types_msg->has_t_enum = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->has_t_enum, "has enum (choice 1)");
  testTrue(types_iter_msg->t_enum==types_msg->t_enum, "enum choice 1");

  types_clean_msg();
  types_msg->t_enum = TYPES__CHOICES__c2;
  types_msg->has_t_enum = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->has_t_enum, "has enum (choice 2)");
  testTrue(types_iter_msg->t_enum==types_msg->t_enum, "enum choice 2");

  types_clean_msg();
  types_msg->t_enum = TYPES__CHOICES__c3;
  types_msg->has_t_enum = 1;
  types_pack_and_iter_unpack();
  testTrue(types_iter_msg->has_t_enum, "has enum (choice 3)");
  testTrue(types_iter_msg->t_enum==types_msg->t_enum, "enum choice 3");

  // string
  types_clean_msg();
  types_msg->t_string = "hello world";
  types_pack_and_iter_unpack();
  if (testTrue(types_iter_msg->t_string != NULL, "has string")) {
    testTrue(strcmp(types_iter_msg->t_string, types_msg->t_string)==0, "string value");
  }

  // bytes
  types_clean_msg();
  types_msg->has_t_bytes = 1;
  types_msg->t_bytes.len = 12;
  char* hw = "world hello";
  types_msg->t_bytes.data = (uint8_t*)hw;
  types_pack_and_iter_unpack();
  if (testTrue(types_iter_msg->has_t_bytes ==  types_msg->has_t_bytes, "has bytes")) {
    if (testTrue(types_iter_msg->t_bytes.len ==  types_msg->t_bytes.len, "bytes len")) {
      testTrue(memcmp(types_iter_msg->t_bytes.data, types_msg->t_bytes.data, types_msg->t_bytes.len) == 0, "bytes value");
    }
  }

  // submessage
  types_clean_msg();
  Types__SubMessage sub;
  types__sub_message__init(&sub);
  sub.has_t_submessage_uint32 = 1;
  sub.t_submessage_uint32 = 42;
  types_msg->t_submessage = &sub;
  types_pack_and_iter_unpack();
  if (testTrue(types_iter_msg->t_submessage!=NULL, "has submessage")) {
    if (testTrue(types_iter_msg->t_submessage->has_t_submessage_uint32, "submessage has uint32 field")) {
      testTrue(types_iter_msg->t_submessage->t_submessage_uint32==42, "submessage uint32 field value");
    }
  }

  // repeated uint32
  types_clean_msg();
  uint32_t repeated_data[5] = {42, 100500, 1024, 65000, 89555};
  types_msg->n_t_repeated_uint32 = 5;
  types_msg->t_repeated_uint32 = repeated_data;
  types_pack_and_iter_unpack();
  if (testTrue(types_iter_msg->n_t_repeated_uint32==5, "repeated uint32 size")) {
    testTrue(types_iter_msg->t_repeated_uint32[0]==42, "repeated uint32 item 0");
    testTrue(types_iter_msg->t_repeated_uint32[1]==100500, "repeated uint32 item 1");
    testTrue(types_iter_msg->t_repeated_uint32[4]==89555, "repeated uint32 item 4");
  }

  // repeated string
  types_clean_msg();
  types_msg->n_t_repeated_string = 2;
  types_msg->t_repeated_string = (char**)malloc(sizeof(char*)*2);
  types_msg->t_repeated_string[0] = "hello world";
  types_msg->t_repeated_string[1] = "42";
  types_pack_and_iter_unpack();
  free(types_msg->t_repeated_string);
  testTrue(types_iter_msg->n_t_repeated_string==2, "repeated string size");
  testTrue(strcmp(types_iter_msg->t_repeated_string[0], "hello world") == 0, "repeated string item 0");
  testTrue(strcmp(types_iter_msg->t_repeated_string[1], "42") == 0, "repeated string item 1");


  // repeated bytes
  types_clean_msg();
  types_msg->t_repeated_bytes = (ProtobufCBinaryData*)malloc(sizeof(ProtobufCBinaryData)*2);
  types_msg->n_t_repeated_bytes = 2;

  types_msg->t_repeated_bytes[0].len = sizeof(BINARY_HELLO_WORLD);
  types_msg->t_repeated_bytes[0].data = malloc(sizeof(BINARY_HELLO_WORLD)*sizeof(uint8_t));
  memcpy(types_msg->t_repeated_bytes[0].data, BINARY_HELLO_WORLD, sizeof(BINARY_HELLO_WORLD));

  types_msg->t_repeated_bytes[1].len = sizeof(BINARY_42);
  types_msg->t_repeated_bytes[1].data = malloc(sizeof(BINARY_42)*sizeof(uint8_t));
  memcpy(types_msg->t_repeated_bytes[1].data, BINARY_42, sizeof(BINARY_42));

  types_pack_and_iter_unpack();
  free(types_msg->t_repeated_bytes[0].data);
  free(types_msg->t_repeated_bytes[1].data);
  free(types_msg->t_repeated_bytes);

  testTrue(unpack_result > 2, "unpack repeated bytes");
  testTrue(types_iter_msg->n_t_repeated_bytes==2, "repeated bytes count");
  testTrue(types_iter_msg->t_repeated_bytes[0].len==11, "repeated bytes [0] len");
  testTrue(types_iter_msg->t_repeated_bytes[1].len==2, "repeated bytes [1] len");
  testTrue(memcmp(types_iter_msg->t_repeated_bytes[0].data, BINARY_HELLO_WORLD, 11) == 0, "repeated bytes [0] value");
  testTrue(memcmp(types_iter_msg->t_repeated_bytes[1].data, BINARY_42, 2) == 0, "repeated bytes [1] value");



  // packed fields unpack
  PackedField* c_packed = packed_field__unpack(NULL, sizeof(REPEATED_PACKED), REPEATED_PACKED);
  PackedField* f_packed = (PackedField*)iter_msg_buffer;
  unpack_result = packed_field__iter_unpack(REPEATED_PACKED, sizeof(REPEATED_PACKED), iter_msg_buffer, sizeof(iter_msg_buffer));
  testTrue(unpack_result>=0, "unpack repeated packed");
  if (testTrue( (f_packed->n_f1==6) && (f_packed->n_f1==c_packed->n_f1), "packed repeated len")) {
    testTrue(f_packed->f1[0]==10, "packed repeated item 0");
    testTrue(f_packed->f1[5]==408, "packed repeated item 5");
  }

  MergeTest* c_merge = merge_test__unpack(NULL, sizeof(MERGE_TEST), MERGE_TEST);
  MergeTest* f_merge = (MergeTest*)iter_msg_buffer;
  unpack_result = merge_test__iter_unpack(MERGE_TEST, sizeof(MERGE_TEST), iter_msg_buffer, sizeof(iter_msg_buffer));

  testTrue( (c_merge->submsg->f1==777) && (c_merge->submsg->f2==0), "protobuf-c not merged submessage");
  testTrue( (f_merge->submsg->f1==777) && (f_merge->submsg->f2==42), "protobuf-c-iter-unpack merged submessage");

  return 1;
}
示例#25
0
int test_unpack_empty_message() {
  int unpack_result = types__iter_unpack(TRUNCATED_VARINT, 0, iter_msg_buffer, sizeof(iter_msg_buffer));
  testTrue(unpack_result == sizeof(Types), "unpack empty message");

  return 1;
}