int and(char *param, int fdwrite, char code) { char nb; t_arg arg[3]; int i; char *tmp; if (write(fdwrite, &code, 1) == -1) return (-1); i = 7; nb = 0; tmp = param; if ((get_args(&arg[0], &nb, &i, tmp)) != 2) tmp++; while (*tmp != ',') tmp++; tmp++; if ((get_args(&arg[1], &nb, &i, tmp)) != 2) tmp++; while (*tmp != ',') tmp++; tmp++; if ((get_args(&arg[2], &nb, &i, tmp)) != 2) tmp++; return (and2(fdwrite, nb, arg)); }
TEST( AllElemMatchOp, MatchesElement ) { BSONObj baseOperanda1 = BSON( "a" << 1 ); auto_ptr<ComparisonMatchExpression> eqa1( new ComparisonMatchExpression() ); ASSERT( eqa1->init( "a", ComparisonMatchExpression::EQ, baseOperanda1[ "a" ] ).isOK() ); BSONObj baseOperandb1 = BSON( "b" << 1 ); auto_ptr<ComparisonMatchExpression> eqb1( new ComparisonMatchExpression() ); ASSERT( eqb1->init( "b", ComparisonMatchExpression::EQ, baseOperandb1[ "b" ] ).isOK() ); auto_ptr<AndMatchExpression> and1( new AndMatchExpression() ); and1->add( eqa1.release() ); and1->add( eqb1.release() ); // and1 = { a : 1, b : 1 } auto_ptr<ElemMatchObjectMatchExpression> elemMatch1( new ElemMatchObjectMatchExpression() ); elemMatch1->init( "x", and1.release() ); // elemMatch1 = { x : { $elemMatch : { a : 1, b : 1 } } } BSONObj baseOperanda2 = BSON( "a" << 2 ); auto_ptr<ComparisonMatchExpression> eqa2( new ComparisonMatchExpression() ); ASSERT( eqa2->init( "a", ComparisonMatchExpression::EQ, baseOperanda2[ "a" ] ).isOK() ); BSONObj baseOperandb2 = BSON( "b" << 2 ); auto_ptr<ComparisonMatchExpression> eqb2( new ComparisonMatchExpression() ); ASSERT( eqb2->init( "b", ComparisonMatchExpression::EQ, baseOperandb2[ "b" ] ).isOK() ); auto_ptr<AndMatchExpression> and2( new AndMatchExpression() ); and2->add( eqa2.release() ); and2->add( eqb2.release() ); auto_ptr<ElemMatchObjectMatchExpression> elemMatch2( new ElemMatchObjectMatchExpression() ); elemMatch2->init( "x", and2.release() ); // elemMatch2 = { x : { $elemMatch : { a : 2, b : 2 } } } AllElemMatchOp op; op.init( "" ); op.add( elemMatch1.release() ); op.add( elemMatch2.release() ); BSONObj nonArray = BSON( "x" << 4 ); ASSERT( !op.matchesSingleElement( nonArray[ "x" ] ) ); BSONObj emptyArray = BSON( "x" << BSONArray() ); ASSERT( !op.matchesSingleElement( emptyArray[ "x" ] ) ); BSONObj nonObjArray = BSON( "x" << BSON_ARRAY( 4 ) ); ASSERT( !op.matchesSingleElement( nonObjArray[ "x" ] ) ); BSONObj singleObjMatch = BSON( "x" << BSON_ARRAY( BSON( "a" << 1 << "b" << 1 ) ) ); ASSERT( !op.matchesSingleElement( singleObjMatch[ "x" ] ) ); BSONObj otherObjMatch = BSON( "x" << BSON_ARRAY( BSON( "a" << 2 << "b" << 2 ) ) ); ASSERT( !op.matchesSingleElement( otherObjMatch[ "x" ] ) ); BSONObj bothObjMatch = BSON( "x" << BSON_ARRAY( BSON( "a" << 1 << "b" << 1 ) << BSON( "a" << 2 << "b" << 2 ) ) ); ASSERT( op.matchesSingleElement( bothObjMatch[ "x" ] ) ); BSONObj noObjMatch = BSON( "x" << BSON_ARRAY( BSON( "a" << 1 << "b" << 2 ) << BSON( "a" << 2 << "b" << 1 ) ) ); ASSERT( !op.matchesSingleElement( noObjMatch[ "x" ] ) ); }
int old_main( int args, char **argv ){ /* Useful cull test cases: one literal T,F,BTerm one AND SOR in DNF SOR not in DNF SAND not in DNF */ //To quiet compiler args = 1; char ** argv2 = argv; argv2 = argv2; //Test SymTree SymTree * st1 = new SymTree(1); st1 = and2(st1,new SymTree(2)); st1 = or2(st1,3); st1 = or2(st1,4); SymTree * st2 = new SymTree(10); st2 = or2(st2,11); st2 = or2(st2,12); st1 = or2(st1,new SymTree(st2)); cout << "AND and OR tests" << endl; st1->printTree(); SymTree *st3; st3 = new SymTree(20); st3 = and2(st3,21); st3 = and2(st3,22); st3 = and2(st3,23); st3->printTree(); delete st3; st1 = and2(st1,st2); st2 = 0; st1->printTree(); //Should have been 20^21^22^(1\/2\/3\/4\/10\/11\/12) SymTree *tmp; cout << "TEST and2\n"; tmp = and2(1,2); tmp->printTree(); delete tmp; tmp = 0; cout << "TEST copy"; SymTree *st1c = new SymTree(st1); st1 = or2(st1, and2(30,31) ); st1c->printTree(); delete st1c; st1c = 0; st1->printTree(); delete st1; st1 = 0; cout << "TEST remove duplicate terms from SymNode"<<endl; SymNode * root = new SymNode(SAND, -1); root->addChild(new SymNode(BTERM, 1)); noDuplicateNodes(root->children); cout <<"when only 1 node is present" <<endl; root->printTree(0); cout <<"when only 2 nodes are present, same." <<endl; root->addChild(new SymNode(BTERM, 1)); noDuplicateNodes(root->children); root->printTree(0); cout <<"when 3 nodes are present, same." <<endl; root->addChild(new SymNode(BTERM, 1)); root->addChild(new SymNode(BTERM, 1)); root->addChild(new SymNode(BTERM, 1)); noDuplicateNodes(root->children); root->printTree(0); cout <<"when 4 nodes are present, 3 same." <<endl; root->addChild(new SymNode(BTERM, 1)); root->addChild(new SymNode(BTERM, 2)); root->addChild(new SymNode(BTERM, 1)); root->addChild(new SymNode(BTERM, 1)); noDuplicateNodes(root->children); root->printTree(0); root->destroyTree(); root = 0; cout << "TEST toDNF "<< endl; SymTree *dnfTest = new SymTree(1); cout << "simple singleton\nbefore:"<< endl; dnfTest->printTree(); dnfTest->toDNF(); cout <<"after"<<endl; dnfTest->printTree(); testIterator(dnfTest); cout << "simple 2-and\nbefore:"<< endl; dnfTest = and2( dnfTest,2); dnfTest->printTree(); dnfTest->toDNF(); cout <<"after"<<endl; dnfTest->printTree(); testIterator(dnfTest); cout << "simple 3-and\nbefore:"<< endl; dnfTest = and2(dnfTest,3); dnfTest->printTree(); dnfTest->toDNF(); cout <<"after"<<endl; dnfTest->printTree(); cout << "simple 4-and\nbefore:"<< endl; dnfTest = and2(dnfTest,4); dnfTest->printTree(); dnfTest->toDNF(); cout <<"after"<<endl; dnfTest->printTree(); cout << "(5 or (and 1 2 3 4))\nbefore:"<< endl; dnfTest = or2(dnfTest,5); dnfTest->printTree(); dnfTest->toDNF(); dnfTest->printTree(); cout << "(or 5 6 (and 1 2 3 4))\nbefore:"<< endl; dnfTest = or2(dnfTest,6); dnfTest->printTree(); dnfTest->toDNF(); dnfTest->printTree(); cout << "(or 5 6 7 (and 1 2 3 4))\nbefore:"<< endl; dnfTest = or2(dnfTest,7); dnfTest->printTree(); dnfTest->toDNF(); dnfTest->printTree(); testIterator(dnfTest); cout << "(and 8 (or 5 6 ))\nbefore:"<< endl; SymTree *dnfTestPrime = and2( new SymTree(8),or2(5,6)); dnfTestPrime->printTree(); dnfTestPrime->toDNF(); dnfTestPrime->printTree(); testIterator(dnfTestPrime); delete dnfTestPrime; dnfTestPrime = 0; cout << "(and 8 (or 5 6 7(and 1 2 3 4)))\nbefore:"<< endl; dnfTest = and2(dnfTest,8); dnfTest->printTree(); dnfTest->toDNF(); dnfTest->printTree(); cout << "(and 8 9 (or 5 6 7(and 1 2 3 4)))\nbefore:"<< endl; //This below is a massive memory leak // BK comment: only leak in main (8/7/2007) SymTree *dnfTestSecond = and2(1,2)->sand(and2( 3,4))->sor(7)->sor(or2(5,6))->sand(and2(8,9)); dnfTestSecond->printTree(); cout << "after" <<endl; dnfTestSecond->toDNF(); dnfTestSecond->printTree(); cout << "(and 8 8 9 (or 5 6 7(and 1 2 3 4)))\nbefore:"<< endl; dnfTestSecond = and2(dnfTestSecond,8); dnfTestSecond->printTree(); cout << "after" <<endl; dnfTestSecond->toDNF(); dnfTestSecond->printTree(); cout << "\nCULLING Test:\nbefore" << endl; dnfTestSecond = or2(dnfTestSecond , and2( and2(13,12),and2(13,12)) ); dnfTestSecond->printTree(); dnfTestSecond->cull(); cout << "after" << endl; dnfTestSecond->printTree(); testIterator(dnfTest); delete dnfTest; dnfTest = NULL; delete dnfTestSecond; dnfTestSecond = 0; cout << "END TEST"<< endl;; // Success Case cout << "Success CASE" << endl; SymTree * tree = new SymTree(); SymTree* children = new SymTree(4); SymTree* children2 = children->sand(5); delete children; children = children2; SymTree* tree2 = tree->sor(children); delete children; delete tree; tree=tree2; //delete children2; children = NULL; children = new SymTree(6); children2 = children->sand(7); delete children; children = children2; tree2 = tree->sor(children); delete children; delete tree; tree = tree2; children = new SymTree(6); children2 = children->sor(7); delete children; children = children2; tree2 = tree->sand(children); delete children; delete tree; tree = tree2; tree->printTree(); cout << "toDNF" << endl; tree->toDNF(); tree->printTree(); cout << "cull" << endl; tree->cull(); tree->printTree(); cout << "END Success CASE" << endl << endl << endl; delete tree; SymTree *myTree = and2( and2(or2((BTerm)0,1),1),and2((BTerm)0,1)); myTree->printTree(); myTree->toDNF(); myTree->printTree(); myTree->cull(); myTree->printTree(); delete myTree; // Fail Case cout << "FAIL CASE:" << endl; SymTree* testCase = new SymTree(); // Valid options (i.e. solutions) SymTree* myAnd = new SymTree(); myAnd = and2((BTerm)0,5); testCase = or2(testCase, myAnd); myAnd = and2((BTerm)0,15); testCase = or2(testCase, myAnd); myAnd = and2((BTerm)0,7); testCase = or2(testCase, myAnd); myAnd = and2((BTerm)0,13); testCase = or2(testCase, myAnd); myAnd = and2(5,20); testCase = or2(testCase, myAnd); myAnd = and2(15,20); testCase = or2(testCase, myAnd); myAnd = and2(7,20); testCase = or2(testCase, myAnd); myAnd = and2(13,20); testCase = or2(testCase, myAnd); myAnd = and2(4,5); testCase = or2(testCase, myAnd); myAnd = and2(4,15); testCase = or2(testCase, myAnd); myAnd = and2(4,7); testCase = or2(testCase, myAnd); myAnd = and2(4,13); testCase = or2(testCase, myAnd); myAnd = and2(5,16); testCase = or2(testCase, myAnd); myAnd = and2(15,16); testCase = or2(testCase, myAnd); myAnd = and2(7,16); testCase = or2(testCase, myAnd); myAnd = and2(13,16); testCase = or2(testCase, myAnd); // All possible haplotype assignments for Father // SymTree* myAnd = new SymTree(); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)7); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)6); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)1); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)0); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)3); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)2); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)6,(BTerm)1); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)7,(BTerm)0); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)13); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)12); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)15); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)14); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)6,(BTerm)13); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)7,(BTerm)12); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)9); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)8); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)11); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)10); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)6,(BTerm)9); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)7,(BTerm)8); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)0,(BTerm)13); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)1,(BTerm)12); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)0,(BTerm)15); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)1,(BTerm)14); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)2,(BTerm)13); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)3,(BTerm)12); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)21); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)20); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)23); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)22); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)6,(BTerm)21); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)7,(BTerm)20); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)17); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)16); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)19); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)18); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)6,(BTerm)17); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)7,(BTerm)16); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)0,(BTerm)21); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)1,(BTerm)20); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)0,(BTerm)23); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)1,(BTerm)22); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)2,(BTerm)21); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)3,(BTerm)20); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)29); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)28); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)31); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)30); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)6,(BTerm)29); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)7,(BTerm)28); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)25); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)24); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)4,(BTerm)27); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)5,(BTerm)26); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)6,(BTerm)25); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)7,(BTerm)24); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)0,(BTerm)29); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)1,(BTerm)28); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)0,(BTerm)31); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)1,(BTerm)30); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)2,(BTerm)29); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)3,(BTerm)28); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)12,(BTerm)21); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)13,(BTerm)20); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)12,(BTerm)23); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)13,(BTerm)22); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)14,(BTerm)21); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)15,(BTerm)20); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)12,(BTerm)17); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)13,(BTerm)16); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)12,(BTerm)19); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)13,(BTerm)18); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)14,(BTerm)17); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)15,(BTerm)16); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)8,(BTerm)21); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)9,(BTerm)20); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)8,(BTerm)23); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)9,(BTerm)22); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)10,(BTerm)21); // testCase = or2(testCase, myAnd); // myAnd = and2((BTerm)11,(BTerm)20); // testCase = or2(testCase, myAnd); SymTree* testCase2 = new SymTree(); SymTree* myOr = new SymTree(); myOr = or2(or2(or2(5,15), 7), 13); testCase2 = and2(testCase2, myOr); myOr = or2(or2(or2(5,15), 7), 13); testCase2 = and2(testCase2, myOr); myOr = or2(or2(or2((BTerm) 0,20), 4), 16); testCase2 = and2(testCase2, myOr); myOr = or2(or2(or2(or2(or2(or2(or2((BTerm) 0, 21), 1), 20), 4), 17), 5), 16); testCase2 = and2(testCase2, myOr); myOr = or2(or2(or2(or2(or2(or2(or2(4, 15), 5), 14), 6), 13), 7), 12); testCase2 = and2(testCase2, myOr); myOr = or2(or2(or2(or2(or2(or2(or2(4, 15), 5), 14), 6), 13), 7), 12); testCase2 = and2(testCase2, myOr); cout << " first step toDNF and cull" << endl; testCase2->toDNF(); testCase2->cull(); testCase = and2(testCase, testCase2); testCase->printTree(); printf("BIG ToDNF\n"); testCase->toDNF(); printf("BIG Cull\n"); testCase->cull(); testCase->printTree(); cout << "END FAIL CASE" << endl << endl << endl; delete testCase; return 0; }