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

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_ValueArray.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:39  rahulj
  64.  * Year 2K copyright swat.
  65.  *
  66.  * Revision 1.2  2000/01/19 00:59:06  roddey
  67.  * Get rid of dependence on old utils output streams.
  68.  *
  69.  * Revision 1.1.1.1  1999/11/09 01:02:07  twl
  70.  * Initial checkin
  71.  *
  72.  * Revision 1.2  1999/11/08 20:42:28  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/ValueArrayOf.hpp>
  81. #include <util/ArrayIndexOutOfBoundsException.hpp>
  82. // ---------------------------------------------------------------------------
  83. //  Force a full instantiation of our array and its enumerator, just to
  84. //  insure that all methods get instantiated and compiled.
  85. // ---------------------------------------------------------------------------
  86. template ValueArrayOf<int>;
  87. template ValueArrayEnumerator<int>;
  88. // ---------------------------------------------------------------------------
  89. //  Local functions
  90. // ---------------------------------------------------------------------------
  91. static bool constructorTests()
  92. {
  93.     // Do a basic constructor with just the count of elements
  94.     ValueArrayOf<double> testArray1(255);
  95.     // Make sure that it has the right initial size
  96.     if (testArray1.length() != 255)
  97.     {
  98.         std::wcout  << L"    The ctor created wrong length() value"
  99.                     << std::endl;
  100.         return false;
  101.     }
  102.     // Copy construct another array from it and test the length
  103.     ValueArrayOf<double> testArray2(testArray1);
  104.     if (testArray2.length() != 255)
  105.     {
  106.         std::wcout << L"    The copy ctor created wrong length() value"
  107.                 << std::endl;
  108.         return false;
  109.     }
  110.     // Test the equality of the two arrays
  111.     if (testArray1 != testArray2)
  112.     {
  113.         std::wcout  << L"    The copy ctor created unequal arrays"
  114.                     << std::endl;
  115.         return false;
  116.     }
  117.     //
  118.     //  Do another one where we provide the initial values.
  119.     //
  120.     double initValues[] = { 1.1, 2.2, 3.3, 4.4 };
  121.     ValueArrayOf<double> testArray3(initValues, 4);
  122.     if (testArray3.length() != 4)
  123.     {
  124.         std::wcout  << L"    The init values ctor created wrong length() value"
  125.                     << std::endl;
  126.         return false;
  127.     }
  128.     // Make sure the initial values are correct
  129.     if ((testArray3[0] != 1.1)
  130.     ||  (testArray3[1] != 2.2)
  131.     ||  (testArray3[2] != 3.3)
  132.     ||  (testArray3[3] != 4.4))
  133.     {
  134.         std::wcout  << L"    The init values ctor did not init contents correctly"
  135.                     << std::endl;
  136.         return false;
  137.     }
  138.     //
  139.     //  Create another array of a different size and assign one of the
  140.     //  existing ones to it and make sure that they are equal.
  141.     //
  142.     ValueArrayOf<double> testArray4(15);
  143.     testArray4 = testArray3;
  144.     if (testArray4 != testArray3)
  145.     {
  146.         std::wcout  << L"    Assignment did not create equal arrays"
  147.                     << std::endl;
  148.         return false;
  149.     }
  150.     return true;
  151. }
  152. static bool accessTests()
  153. {
  154.     ValueArrayOf<unsigned int> testArray1(16);
  155.     // Fill in the array
  156.     unsigned int index;
  157.     for (index = 0; index < 16; index++)
  158.         testArray1[index] = index;
  159.     // Read them back again
  160.     for (index = 0; index < 16; index++)
  161.     {
  162.         if (testArray1[index] != index)
  163.         {
  164.             std::wcout  << L"    Failed to read back values just set"
  165.                         << std::endl;
  166.             return false;
  167.         }
  168.     }
  169.     // Make sure we get the expected array index error
  170.     bool caughtIt = false;
  171.     try
  172.     {
  173.         index = testArray1[16];
  174.     }
  175.     catch(const ArrayIndexOutOfBoundsException&)
  176.     {
  177.         caughtIt = true;
  178.     }
  179.     if (!caughtIt)
  180.     {
  181.         std::wcout << L"    Failed to catch index error" << std::endl;
  182.         return false;
  183.     }
  184.     return true;
  185. }
  186. // ---------------------------------------------------------------------------
  187. //  Test entry point
  188. // ---------------------------------------------------------------------------
  189. bool testValueArray()
  190. {
  191.     std::wcout  << L"----------------------------------n"
  192.                 << L"Testing ValueArrayOf template classn"
  193.                 << L"----------------------------------" << std::endl;
  194.     bool retVal = true;
  195.     try
  196.     {
  197.         // Call other local methods to do specific tests
  198.         std::wcout << L"Testing ValueArrayOf contructors" << std::endl;
  199.         if (!constructorTests())
  200.         {
  201.             std::wcout  << L"ValueArrayOf constructor tests failed"
  202.                         << std::endl;
  203.             retVal = false;
  204.         }
  205.          else
  206.         {
  207.             std::wcout  << L"ValueArrayOf constructor tests passed"
  208.                         << std::endl;
  209.         }
  210.         std::wcout << std::endl;
  211.         std::wcout << L"Testing ValueArrayOf element access" << std::endl;
  212.         if (!accessTests())
  213.         {
  214.             std::wcout  << L"ValueArrayOf element access tests failed"
  215.                         << std::endl;
  216.             retVal = false;
  217.         }
  218.          else
  219.         {
  220.             std::wcout  << L"ValueArrayOf element access tests passed"
  221.                         << std::endl;
  222.         }
  223.         std::wcout << std::endl;
  224.     }
  225.     catch(const XMLException& toCatch)
  226.     {
  227.         std::wcout  << L"  ERROR: Unexpected exception!n   Msg: "
  228.                     << toCatch.getMessage() << std::endl;
  229.         return false;
  230.     }
  231.     return retVal;
  232. }