CoreTests_ValueVector.cpp
上传用户:huihehuasu
上传日期:2007-01-10
资源大小:6948k
文件大小:13k
源码类别:

xml/soap/webservice

开发平台:

C/C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  * 
  4.  * Copyright (c) 1999-2000 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  * 
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer. 
  13.  * 
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  * 
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:  
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  * 
  26.  * 4. The names "Xerces" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written 
  29.  *    permission, please contact apache@apache.org.
  30.  * 
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  * 
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  * 
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation, and was
  51.  * originally based on software copyright (c) 1999, International
  52.  * Business Machines, Inc., http://www.ibm.com .  For more information
  53.  * on the Apache Software Foundation, please see
  54.  * <http://www.apache.org/>.
  55.  */
  56. /*
  57.  * $Log: CoreTests_ValueVector.cpp,v $
  58.  * Revision 1.4  2000/03/02 19:55:49  roddey
  59.  * This checkin includes many changes done while waiting for the
  60.  * 1.1.0 code to be finished. I can't list them all here, but a list is
  61.  * available elsewhere.
  62.  *
  63.  * Revision 1.3  2000/02/06 07:48:40  rahulj
  64.  * Year 2K copyright swat.
  65.  *
  66.  * Revision 1.2  2000/01/19 00:59:07  roddey
  67.  * Get rid of dependence on old utils output streams.
  68.  *
  69.  * Revision 1.1.1.1  1999/11/09 01:02:10  twl
  70.  * Initial checkin
  71.  *
  72.  * Revision 1.2  1999/11/08 20:42:29  rahul
  73.  * Swat for adding in Product name and CVS comment log variable.
  74.  *
  75.  */
  76. // ---------------------------------------------------------------------------
  77. //  XML4C2 includes
  78. // ---------------------------------------------------------------------------
  79. #include "CoreTests.hpp"
  80. #include <util/ValueVectorOf.hpp>
  81. #include <util/ArrayIndexOutOfBoundsException.hpp>
  82. // ---------------------------------------------------------------------------
  83. //  Force a full instantiation of our vector and its enumerator, just to
  84. //  insure that all methods get instantiated and compiled.
  85. // ---------------------------------------------------------------------------
  86. template ValueVectorOf<int>;
  87. template ValueVectorEnumerator<int>;
  88. // ---------------------------------------------------------------------------
  89. //  Templatized testing code. These allow the exact same tests to be run
  90. //  for any number of instantiation types over the by value vectors.
  91. // ---------------------------------------------------------------------------
  92. template <class T> bool commonValueTests()
  93. {
  94.     const unsigned int  testMax = 3;
  95.     bool                caughtIt;
  96.     // Create a vector of testMax of the instantiation type
  97.     ValueVectorOf<T> testVec(testMax);
  98.     // Make sure the initial capacity is what we set
  99.     if (testVec.curCapacity() != testMax)
  100.     {
  101.         std::wcout << L"   Init capacity was bad" << std::endl;
  102.         return false;
  103.     }
  104.     // Make sure the initial size is zero
  105.     if (testVec.size() != 0)
  106.     {
  107.         std::wcout << L"   Init size was bad" << std::endl;
  108.         return false;
  109.     }
  110.     // Test value for adding
  111.     T testElem;
  112.     // Add a value and check the count is 1
  113.     testVec.addElement(testElem);
  114.     if (testVec.size() != 1)
  115.     {
  116.         std::wcout << L"   Adding one element caused bad size" << std::endl;
  117.         return false;
  118.     }
  119.     // Add another value and check the count is 2
  120.     testVec.addElement(testElem);
  121.     if (testVec.size() != 2)
  122.     {
  123.         std::wcout << L"   Adding another element caused bad size" << std::endl;
  124.         return false;
  125.     }
  126.     // Test that the two of them are the same
  127.     if (testVec.elementAt(0) != testVec.elementAt(1))
  128.     {
  129.         std::wcout << L"   First two elements did not match" << std::endl;
  130.         return false;
  131.     }
  132.     // Add two more, which should cause an expansion of the vector
  133.     testVec.addElement(testElem);
  134.     testVec.addElement(testElem);
  135.     if (testVec.curCapacity() == testMax)
  136.     {
  137.         std::wcout  << L"   Adding another element failed to cause an expansion"
  138.                     << std::endl;
  139.         return false;
  140.     }
  141.     // Check that we get an array bounds exception after an expansion
  142.     caughtIt = false;
  143.     try
  144.     {
  145.         testVec.elementAt(4);
  146.     }
  147.     catch(const ArrayIndexOutOfBoundsException&)
  148.     {
  149.         caughtIt = true;
  150.     }
  151.     if (!caughtIt)
  152.     {
  153.         std::wcout  << L"   Failed to catch array bounds error at element 4"
  154.                     << std::endl;
  155.         return false;
  156.     }
  157.     // Remove an item and see if the count went down by one
  158.     testVec.removeElementAt(0);
  159.     if (testVec.size() != 3)
  160.     {
  161.         std::wcout  << L"   Removing an element did not adjust size correctly"
  162.                     << std::endl;
  163.         return false;
  164.     }
  165.     // Remove the rest of them and make sure we hit zero
  166.     testVec.removeElementAt(0);
  167.     testVec.removeElementAt(0);
  168.     testVec.removeElementAt(0);
  169.     if (testVec.size() != 0)
  170.     {
  171.         std::wcout  << L"   Removing all elements did not zero the size"
  172.                     << std::endl;
  173.         return false;
  174.     }
  175.     // Check that we get an array bounds exception now still
  176.     caughtIt = false;
  177.     try
  178.     {
  179.         testVec.elementAt(0);
  180.     }
  181.     catch(const ArrayIndexOutOfBoundsException&)
  182.     {
  183.         caughtIt = true;
  184.     }
  185.     if (!caughtIt)
  186.     {
  187.         std::wcout  << L"   Failed to catch array bounds error at element 0"
  188.                     << std::endl;
  189.         return false;
  190.     }
  191.     // Add a few more elements back in, via insertion
  192.     testVec.insertElementAt(testElem, 0);
  193.     testVec.insertElementAt(testElem, 0);
  194.     testVec.insertElementAt(testElem, 0);
  195.     if (testVec.size() != 3)
  196.     {
  197.         std::wcout << L"   Inserting elements caused bad size" << std::endl;
  198.         return false;
  199.     }
  200.     // Now do a remove all elements
  201.     testVec.removeAllElements();
  202.     if (testVec.size() != 0)
  203.     {
  204.         std::wcout << L"   removeAllElements caused bad size" << std::endl;
  205.         return false;
  206.     }
  207.     return true;
  208. }
  209. template <class T> bool extendedValueTests()
  210. {
  211.     const unsigned int testMax = 8;
  212.     // Create a test vector and put in ascending test values
  213.     ValueVectorOf<T> testVec(testMax);
  214.     testVec.addElement(T(0));
  215.     testVec.addElement(T(1));
  216.     testVec.addElement(T(2));
  217.     testVec.addElement(T(3));
  218.     testVec.addElement(T(4));
  219.     testVec.addElement(T(5));
  220.     testVec.addElement(T(6));
  221.     testVec.addElement(T(7));
  222.     // Now check that they went in that way
  223.     unsigned int index;
  224.     for (index = 0; index < testMax; index++)
  225.     {
  226.         if (testVec.elementAt(index) != T(index))
  227.         {
  228.             std::wcout  << L"   addElement put elements in wrong order"
  229.                         << std::endl;
  230.             return false;
  231.         }
  232.     }
  233.     // Remove the zero'th element and test again
  234.     testVec.removeElementAt(0);
  235.     for (index = 0; index < testMax-1; index++)
  236.     {
  237.         if (testVec.elementAt(index) != T(index+1))
  238.         {
  239.             std::wcout  << L"   removeElement at head removed wrong element"
  240.                         << std::endl;
  241.             return false;
  242.         }
  243.     }
  244.     // Test edge case by removing last element and test again
  245.     testVec.removeElementAt(6);
  246.     for (index = 0; index < testMax-2; index++)
  247.     {
  248.         if (testVec.elementAt(index) != T(index+1))
  249.         {
  250.             std::wcout  << L"   removeElement at end removed wrong element"
  251.                         << std::endl;
  252.             return false;
  253.         }
  254.     }
  255.     return true;
  256. }
  257. // ---------------------------------------------------------------------------
  258. //  Local functions
  259. // ---------------------------------------------------------------------------
  260. static bool doBasicTests()
  261. {
  262.     bool retVal = true;
  263.     //
  264.     // Do the common value vector tests for ints, bools and strings.
  265.     //
  266.     std::wcout << L"Testing ValueVectorOf<int>, common tests" << std::endl;
  267.     if (!commonValueTests<int>())
  268.     {
  269.         std::wcout << L"ValueVectorOf<int> failed" << std::endl;
  270.         retVal = false;
  271.     }
  272.      else
  273.     {
  274.         std::wcout << L"ValueVectorOf<int> passed" << std::endl;
  275.     }
  276.     std::wcout << std::endl;
  277.     std::wcout << L"Testing ValueVectorOf<bool>, common tests" << std::endl;
  278.     if (!commonValueTests<bool>())
  279.     {
  280.         std::wcout << L"ValueVectorOf<bool> failed" << std::endl;
  281.         retVal = false;
  282.     }
  283.      else
  284.     {
  285.         std::wcout << L"ValueVectorOf<bool> passed" << std::endl;
  286.     }
  287.     std::wcout << std::endl;
  288.     //
  289.     //  And now do the second round of extended tests. These require that
  290.     //  the instantiation type be of a fundamental value, because its going
  291.     //  to test element ordering issues.
  292.     //
  293.     std::wcout << L"Testing ValueVectorOf<int>, extended tests" << std::endl;
  294.     if (!extendedValueTests<int>())
  295.     {
  296.         std::wcout << L"Extended ValueVectorOf<int> failed" << std::endl;
  297.         retVal = false;
  298.     }
  299.      else
  300.     {
  301.         std::wcout << L"Extended ValueVectorOf<int> passed" << std::endl;
  302.     }
  303.     std::wcout << std::endl;
  304.     return retVal;
  305. }
  306. static bool enumTests()
  307. {
  308.     // Create a vector and fill it in with some known values
  309.     ValueVectorOf<unsigned int> testVec(32);
  310.     unsigned int index;
  311.     for (index = 0; index < 32; index++)
  312.         testVec.addElement(index);
  313.     // Create an enumeration for it
  314.     ValueVectorEnumerator<unsigned int> enumTest(&testVec);
  315.     index = 0;
  316.     while (enumTest.hasMoreElements())
  317.     {
  318.         if (enumTest.nextElement() != index++)
  319.         {
  320.             std::wcout  << L"    Enumerator sequence was incorrect"
  321.                         << std::endl;
  322.             return false;
  323.         }
  324.     }
  325.     if (index != 32)
  326.     {
  327.         std::wcout  << L"    Enumerator did not enum enough elements"
  328.                     << std::endl;
  329.         return false;
  330.     }
  331.     return true;
  332. }
  333. // ---------------------------------------------------------------------------
  334. //  Test entry point
  335. // ---------------------------------------------------------------------------
  336. bool testValueVector()
  337. {
  338.     std::wcout  << L"----------------------------------n"
  339.                 << L"Testing ValueVectorOf template classn"
  340.                 << L"----------------------------------" << std::endl;
  341.     bool retVal = true;
  342.     try
  343.     {
  344.         // Do the basic suite of tests, which is templatized
  345.         if (!doBasicTests())
  346.             retVal = false;
  347.         // Test the enumerator
  348.         std::wcout << L"Testing ValueVectorEnumerator" << std::endl;
  349.         if (!enumTests())
  350.         {
  351.             std::wcout << L"ValueVectorEnumeration failed" << std::endl;
  352.             retVal = false;
  353.         }
  354.          else
  355.         {
  356.             std::wcout << L"ValueVectorEnumeration passed" << std::endl;
  357.         }
  358.         std::wcout << std::endl;
  359.     }
  360.     catch(const XMLException& toCatch)
  361.     {
  362.         std::wcout  << L"  ERROR: Unexpected exception!n   Msg: "
  363.                     << toCatch.getMessage() << std::endl;
  364.         return false;
  365.     }
  366.     return retVal;
  367. }