Project

Profile

Help

Revision 306962bc

Added by O'Neil Delpratt 5 months ago

Saxon/C development - Added multi-threading test case and other bug fixes

View differences:

src/main/c/samples/cppTests/testXSLT30.cpp
8 8
#include "cppExtensionFunction.h"
9 9
#include "CppTestUtils.h"
10 10
#include <string>
11
#include <thread>
11 12

  
12
// TODO: write test case for checking parameters which are null
13 13

  
14 14

  
15 15
using namespace std;
......
443 443
        if (trans->exceptionOccurred()) {
444 444
            cerr <<"Error expected: "<< trans->getErrorMessage() << endl;
445 445
        }
446
        trans->exceptionClear();
446 447
        return;
447 448
    }
448 449

  
......
529 530
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
530 531
        if(values == nullptr) {
531 532
            cerr<<"values NULL ====== FAIL ======="<<endl;
533
            processor->exceptionClear();
532 534
        } else {
533 535
            trans->setParameter(name.str().c_str(), values);
534 536
        }
......
545 547
        name1 << "sparam:value" << i;
546 548
        cout << " i:" << i << " Map size:" << parMap.size() << ", ";
547 549
        try {
550

  
548 551
            XdmValue *valuei = parMap.at(name1.str());
549 552
            if (valuei != NULL) {
550 553
                cout << name1.str();
......
553 556
                cout << endl;
554 557
            }
555 558
        } catch (const std::out_of_range &oor) {
556
            cout << "Out of range exception occurred. Exception no. " << endl;
559
            cout << "Out of range exception occurred. Exception " << endl;
557 560
            if (!errorFound) {
558 561
                sresult->success++;
559 562
                errorFound = true;
......
1000 1003
        printf("result is null \nCheck For errors:");
1001 1004
        sresult->failure++;
1002 1005
        sresult->failureList.push_back("xmarkTest1");
1006
        cerr << "xmarkTest1 cp0" << endl;
1003 1007
        if (proc->exceptionOccurred()) {
1004
            cerr << proc->getErrorMessage() << endl;
1008
            cerr << "xmarkTest1 cp1" << endl;
1009
            const char * message = proc->getErrorMessage();
1010
            cerr << "xmarkTest1 cp2" << endl;
1011
            if(message != nullptr) {
1012
                cerr << "xmarkTest1 cp2-1" << endl;
1013
                cerr << proc->getErrorMessage() << endl;
1014
            } else {
1015
                cerr << "Message is nullptr" << endl;
1016
                SaxonProcessor::sxn_environ->env->ExceptionDescribe();
1017
            }
1005 1018
        }
1019
        cerr << "xmarkTest1 cp3" << endl;
1006 1020
    }
1007 1021

  
1008 1022

  
......
1025 1039
        sresult->failure++;
1026 1040
        sresult->failureList.push_back("xmarkTest2");
1027 1041
        if (proc->exceptionOccurred() > 0) {
1028
            cout << proc->getErrorMessage() << endl;
1042
            SaxonApiException * exception = proc->getException();
1043
            if(exception != nullptr) {
1044
                const char *message = exception->getMessage();
1045
                if (message != nullptr) {
1046
                    cerr << "xmarkTest2 cp2-1" << endl;
1047
                    cerr << message << endl;
1048
                }
1049
                delete exception;
1050
            } else {
1051
                cerr << "Exception object is nullptr" << endl;
1052
                SaxonProcessor::sxn_environ->env->ExceptionDescribe();
1053
            }
1029 1054
        }
1030 1055
    }
1031 1056

  
......
1154 1179

  
1155 1180
    if(executable == nullptr) {
1156 1181
        if(trans->exceptionOccurred()) {
1157
            cerr<<"Error: "<<trans->getErrorMessage()<<endl;
1182
            SaxonApiException * exception = trans->getException();
1183
            if(exception != nullptr) {
1184
                cerr << "Error: " << exception->getMessage() << endl;
1185
                delete exception;
1186
            }
1158 1187

  
1159 1188
        }
1160 1189
        sresult->failure++;
1161 1190
        sresult->failureList.push_back("testCallFunction");
1191
        trans->exceptionClear();
1162 1192
        return;
1163 1193

  
1164 1194

  
......
1179 1209
        cout << "testCallFunction ======= FAIL ======" << endl;
1180 1210
        if(executable->exceptionOccurred()) {
1181 1211
            SaxonApiException * exception = executable->getException();
1182
            cerr<<"Error: " << exception->getMessage() <<endl;
1183
            delete exception;
1212
            if(exception != nullptr) {
1213
                cerr << "Error: " << exception->getMessage() << endl;
1214
                delete exception;
1215
            }
1184 1216
        }
1185 1217
        sresult->failure++;
1186 1218
        sresult->failureList.push_back("testCallFunction");
......
1237 1269
        sresult->failure++;
1238 1270
        sresult->failureList.push_back("testResolveUri");
1239 1271
        if(trans->exceptionOccurred()) {
1240
            cout << "Error: "<< trans->getErrorMessage() << endl;
1272
            const char * message = trans->getErrorMessage();
1273
            if(message != nullptr) {
1274
                cout << "Error: " << trans->getErrorMessage() << endl;
1275
            }
1241 1276
        }
1277
        trans->exceptionClear();
1242 1278
        return;
1243 1279
    }
1244 1280
    XdmValue *value = executable->callTemplateReturningValue("go");
......
1253 1289
        const char *svalue = value->itemAt(0)->getStringValue();
1254 1290
        cout << "testResolveUri = " << svalue << endl;
1255 1291
        sresult->success++;
1292
        delete value;
1256 1293
    }
1257 1294

  
1258 1295

  
......
1263 1300

  
1264 1301
    XdmNode *node = proc->parseXmlFromString("<doc><e>text</e></doc>");
1265 1302

  
1266
    XsltExecutable * executable = trans->compileFromString(
1303
    XsltExecutable *executable = trans->compileFromString(
1267 1304
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:variable name='x' select='.'/><xsl:template match='/'>errorA</xsl:template><xsl:template match='e'>[<xsl:value-of select='name($x)'/>]</xsl:template></xsl:stylesheet>");
1268 1305

  
1269
    if(executable == nullptr) {
1306
    if (executable == nullptr) {
1270 1307
        sresult->failure++;
1271 1308
        sresult->failureList.push_back("testContextNotRoot");
1272
        if(trans->exceptionOccurred()) {
1273
            cout << "Error: "<< trans->getErrorMessage() << endl;
1309
        if (trans->exceptionOccurred()) {
1310
            cout << "Error: " << trans->getErrorMessage() << endl;
1274 1311
        }
1275 1312
        return;
1276 1313
    }
1277 1314

  
1278 1315
    executable->setGlobalContextItem(node);
1279 1316
    if (node->getChildCount() > 0) {
1280
        XdmNode *eNode = node->getChildren()[0]->getChildren()[0];
1281
        cout << "Node content = " << eNode->toString() << endl;
1282
        executable->setInitialMatchSelection(eNode);
1283
        const char *result = executable->applyTemplatesReturningString();
1317
        XdmNode **eNodeL1 = node->getChildren();
1318
        if (eNodeL1 != nullptr) {
1319
            XdmNode ** eNodeChildren = eNodeL1[0]->getChildren();
1320
            if (eNodeChildren != nullptr) {
1321
                XdmNode *eNode = eNodeChildren[0];
1322
                cout << "Node content = " << eNode->toString() << endl;
1323
                executable->setInitialMatchSelection(eNode);
1324
                const char *result = executable->applyTemplatesReturningString();
1284 1325

  
1285
        if (result== nullptr) {
1326
                if (result == nullptr) {
1286 1327

  
1287
            cout << "testCallFunction ======= FAIL ======" << endl;
1288
            if(executable->exceptionOccurred()) {
1289
                SaxonApiException * exception = executable->getException();
1290
                cerr<<"Error: " << exception->getMessage() <<endl;
1291
                delete exception;
1292
            }
1293
            sresult->failure++;
1294
            sresult->failureList.push_back("testContextNotRoot");
1295
        } else {
1328
                    cout << "testCallFunction ======= FAIL ======" << endl;
1329
                    if (executable->exceptionOccurred()) {
1330
                        SaxonApiException *exception = executable->getException();
1331
                        cerr << "Error: " << exception->getMessage() << endl;
1332
                        delete exception;
1333
                    }
1334
                    sresult->failure++;
1335
                    sresult->failureList.push_back("testContextNotRoot");
1296 1336

  
1297
            cout << "testContextNotRoot = " << result << endl;
1298
            sresult->success++;
1337

  
1338
                } else {
1339

  
1340
                    cout << "testContextNotRoot = " << result << endl;
1341
                    sresult->success++;
1342
                    delete result;
1343

  
1344
                }
1345

  
1346
            }
1299 1347
        }
1348
    } else {
1349
        sresult->failure++;
1350
        sresult->failureList.push_back("testContextNotRoot");
1300 1351
    }
1352

  
1353
    delete executable;
1354
    delete node;
1301 1355
}
1302 1356

  
1303 1357

  
......
1612 1666

  
1613 1667
}
1614 1668

  
1669
static int NUM_THREADS = 10;
1615 1670

  
1616
/*
1617
 *
1618
 *void *RunThread(void *args) {
1671
void RunThread(XsltExecutable * executable, int tid, const std::string filename) {
1619 1672

  
1620
    struct arg_struct *argsi = (struct arg_struct *)args;
1621
    int threadid = argsi->id;
1622
    Xslt30Processor * trans = argsi->trans;
1623
    long tid;
1624
    tid = (long)threadid;
1673
    cerr<<"RunThread cp0,  THEAD ID="<<tid<<endl;
1625 1674

  
1626
    trans->attachThread();
1675
   executable->setInitialMatchSelectionAsFile(filename.c_str());
1627 1676

  
1628
   trans->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
1677
    cerr<<"RunThread cp1"<<endl;
1678

  
1679
    const char *result = nullptr; executable->applyTemplatesReturningString();
1680
    if(result != nullptr) {
1681
        cout << " Result from THREAD ID: " << tid << ", " << result << endl;
1682
        delete result;
1683
    } else {
1684
        cout << " ===== Failed in THREAD ID: "<< tid << endl;
1685
        /*if(executable->exceptionOccurred()) {
1686
            SaxonApiException *exception = executable->getException();
1687

  
1688
            if (exception != nullptr) {
1689
                const char *message = exception->getMessage();
1690
                if (message != nullptr) {
1691
                    cerr << "Error = " << message << endl;
1692
                }
1693
            }
1694
        } else {*/
1695
            cerr<<"No exception found - result is nullptr"<<endl;
1696
        //}
1697

  
1698
    }
1629 1699

  
1630
    const char *result = trans->applyTemplatesReturningString();
1631
    cout<<" Result from THREAD ID: "<< tid << ", " << result<<endl;
1632
    delete result;
1633
    //pthread_exit(NULL);
1634
    trans->detachThread();
1635
}
1636 1700

  
1637
void testThreads (SaxonProcessor * processor) {
1638
    pthread_t threads[NUM_THREADS];
1639
    int rc;
1640
    int i;
1701
}
1641 1702

  
1642
    Xslt30Processor *  trans = processor->newXslt30Processor();
1703
void testThreads (SaxonProcessor * processor, Xslt30Processor * trans, sResultCount *sresult) {
1643 1704

  
1644
    trans->compileFromFile("../php/xsl/foo.xsl");
1645
    struct arg_struct args;
1646
    args.trans = trans;
1705
    std::vector<std::string> s = {
1706
            "../data/xmark100k.xml",
1707
            "../data/xmark1.xml",
1708
            "../data/xmark4.xml",
1709
            "../data/xmark10.xml"
1710
    };
1647 1711

  
1648
    for( i = 0; i < NUM_THREADS; i++ ) {
1649
        cout << "main() : creating thread, " << i << endl;
1650
        args.id = i;
1651
        rc = pthread_create(&threads[i], NULL, RunThread, (void *)&args);
1712
    std::vector<std::thread> threads;
1652 1713

  
1653
        if (rc) {
1654
            cout << "Error:unable to create thread," << rc << endl;
1655
            exit(-1);
1714
    for (int i = 0; i < s.size(); i++) {
1715
        XsltExecutable * executable = trans->compileFromFile("q12.xsl");
1716
        if(executable != nullptr) {
1717
            threads.push_back(std::thread(RunThread, executable, i, s[i]));
1718
        } else {
1719
            cerr<<"Error : Failed to create thread as executable is NULL"<<endl;
1656 1720
        }
1657
	(void) pthread_join(threads[i], NULL);
1658 1721
    }
1659
  //  pthread_exit(NULL);
1722
    cerr<<"testThreads cp0"<<endl;
1723
    for (auto &th : threads) {
1724
        th.join();
1725
        cerr<<"testThreads cp1 - loop"<<endl;
1726
    }
1660 1727
}
1661 1728

  
1662
 *
1663
 */
1664 1729

  
1665 1730

  
1666 1731
int main(int argc, char* argv[]) {
......
1680 1745
    cout<<"CWD = "<< buff<<endl;
1681 1746
    if(cwd != nullptr) {
1682 1747
   	 processor->setcwd(cwd);//"/Users/ond1/work/development/git/saxon-dev/saxondev/src/main/c/samples/cppTests"); //set to the current working directory
1748
    } else {
1749
        processor->setcwd(buff);
1683 1750
    }
1684 1751
    if (processor->isSchemaAwareProcessor()) {
1685 1752

  
......
1703 1770

  
1704 1771
    testInitialTemplate(processor, trans, sresult);
1705 1772

  
1706

  
1773
    cout<<endl<<"============================================================="<<endl<<endl;
1707 1774

  
1708 1775
    exampleSimple1Err(trans, sresult);
1709 1776

  
1777
    cout<<endl<<"============================================================="<<endl<<endl;
1778

  
1710 1779
    exampleSimple1(trans, sresult);
1711 1780

  
1781
    cout<<endl<<"============================================================="<<endl<<endl;
1782

  
1712 1783
    exampleSimple_xmark(trans, sresult);
1713 1784

  
1785
    cout<<endl<<"============================================================="<<endl<<endl;
1786

  
1714 1787
    exampleSimple2(trans, sresult);
1715 1788

  
1789
    cout<<endl<<"============================================================="<<endl<<endl;
1790

  
1716 1791
    exampleSimple3(processor, trans, sresult);
1717 1792

  
1793
    cout<<endl<<"============================================================="<<endl<<endl;
1794

  
1718 1795
    exampleSimple3aError(processor, trans, sresult);
1719 1796

  
1797
    cout<<endl<<"============================================================="<<endl<<endl;
1798

  
1720 1799
    testApplyTemplatesString1(trans, sresult);
1721 1800

  
1801
    cout<<endl<<"============================================================="<<endl<<endl;
1802

  
1722 1803
    testApplyTemplatesString2(processor, trans, sresult);
1723 1804

  
1805
    cout<<endl<<"============================================================="<<endl<<endl;
1806

  
1807

  
1724 1808
    testApplyTemplates2a_Error(processor, trans, sresult);
1725 1809

  
1810
    cout<<endl<<"============================================================="<<endl<<endl;
1811

  
1726 1812
    testTransformToString4(processor, trans, sresult);
1727 1813

  
1814
    cout<<endl<<"============================================================="<<endl<<endl;
1815

  
1728 1816
    testCatalog(cwd, processor,  sresult);
1729 1817

  
1818
    cout<<endl<<"============================================================="<<endl<<endl;
1819

  
1730 1820
    testTransformToString2b(processor, trans, sresult);
1731 1821

  
1822
    cout<<endl<<"============================================================="<<endl<<endl;
1823

  
1732 1824
    testTransformToString3(processor, trans, sresult);
1825

  
1826
    cout<<endl<<"============================================================="<<endl<<endl;
1733 1827
	
1734 1828
    testTransformFromstring(processor, trans, sresult);
1735 1829

  
1830
    cout<<endl<<"============================================================="<<endl<<endl;
1831

  
1736 1832
    testTransformFromstring2Err(processor, trans, sresult);
1737 1833

  
1834
    cout<<endl<<"============================================================="<<endl<<endl;
1835

  
1738 1836
    testTrackingOfValueReference(processor, trans, sresult);
1739 1837

  
1740
    //testTrackingOfValueReferenceError(processor, trans, sresult);
1838
    cout<<endl<<"============================================================="<<endl<<endl;
1839

  
1840
    testTrackingOfValueReferenceError(processor, trans, sresult);
1841

  
1842
    cout<<endl<<"============================================================="<<endl<<endl;
1741 1843

  
1742 1844
    testPackage1(trans, sresult);
1743 1845

  
1846
    cout<<endl<<"============================================================="<<endl<<endl;
1847

  
1744 1848
    testPackage1a(trans, sresult);
1745 1849

  
1850
    cout<<endl<<"============================================================="<<endl<<endl;
1851

  
1746 1852
    testPackage2_Error(trans, sresult);
1747 1853

  
1854
    cout<<endl<<"============================================================="<<endl<<endl;
1855

  
1748 1856
    testXdmNodeOutput(trans, sresult);
1749 1857

  
1750
    exampleParam(processor, trans, sresult);
1858
    cout<<endl<<"============================================================="<<endl<<endl;
1751 1859

  
1752
    //SaxonProcessor::sxn_environ->env->ExceptionDescribe();
1860
    exampleParam(processor, trans, sresult);
1753 1861

  
1862
    cout<<endl<<"============================================================="<<endl<<endl;
1754 1863

  
1755 1864
    xmarkTest1(trans, sresult);
1756 1865

  
1757
    xmarkTest2(trans, sresult);
1866
    cout<<endl<<"============================================================="<<endl<<endl;
1758 1867

  
1759
    processor->release();
1760
    delete processor;
1868
    xmarkTest2(trans, sresult);
1761 1869

  
1762
    return 0;
1870
    cout<<endl<<"============================================================="<<endl<<endl;
1763 1871

  
1764 1872
    testCallFunction(processor, trans, sresult);
1765 1873

  
1766

  
1874
    cout<<endl<<"============================================================="<<endl<<endl;
1767 1875

  
1768 1876
    testResolveUri(processor, trans, sresult);
1877

  
1878
    cout<<endl<<"============================================================="<<endl<<endl;
1879

  
1769 1880
    testContextNotRoot(processor, trans, sresult);
1881

  
1882
    cout<<endl<<"============================================================="<<endl<<endl;
1883

  
1770 1884
    testContextNotRootNamedTemplate(processor, trans, sresult);
1885

  
1886
    cout<<endl<<"============================================================="<<endl<<endl;
1887

  
1771 1888
    testContextNotRootNamedTemplateValue(processor, trans, sresult);
1889

  
1890
    cout<<endl<<"============================================================="<<endl<<endl;
1891

  
1772 1892
    testPipeline(processor, sresult);
1773 1893

  
1894

  
1895
    cout<<endl<<"============================================================="<<endl<<endl;
1896

  
1897

  
1774 1898
    //Available in PE and EE
1775 1899
    //testTransformToStringExtensionFunc(processor, trans);
1776 1900

  
......
1783 1907

  
1784 1908
    Xslt30Processor *trans2 = processor2->newXslt30Processor();
1785 1909
    testApplyTemplatesString1(trans2, sresult);
1910
    cout<<endl<<"============================================================="<<endl<<endl;
1911

  
1912

  
1913
    testThreads (processor2, trans2, sresult);
1914

  
1915

  
1916

  
1786 1917
    delete trans2;
1918
    delete processor2;
1787 1919
    processor2->release();
1788 1920

  
1921
    cout<<endl<<"======================== Test Results ========================"<<endl<<endl;
1922

  
1789 1923
    std::cout << "\nTest Results - Number of tests= " << (sresult->success + sresult->failure) << ", Successes = "
1790 1924
              << sresult->success << ",  Failures= " << sresult->failure << std::endl;
1791 1925

  
1792 1926
    std::list<std::string>::iterator it;
1793 1927
    std::cout << "Failed tests:" << std::endl;
1794
// Make iterate point to begining and incerement it one by one till it reaches the end of list.
1928
    // Make iterate point to beginning and increment it one by one until it reaches the end of list.
1795 1929
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
1796 1930
        //Print the contents
1797 1931
        std::cout << it->c_str() << std::endl;

Also available in: Unified diff