source: trunk/test_more/simple_vector.cpp @ 2354

Last change on this file since 2354 was 1370, checked in by bradbell, 11 years ago

trunk: Fix svn_add_id.sh and use it set Id property for some missed files.

  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1/* $Id: simple_vector.cpp 1370 2009-05-31 05:31:50Z bradbell $ */
2// BEGIN SHORT COPYRIGHT
3/* --------------------------------------------------------------------------
4CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-06 Bradley M. Bell
5
6CppAD is distributed under multiple licenses. This distribution is under
7the terms of the
8                    Common Public License Version 1.0.
9
10A copy of this license is included in the COPYING file of this distribution.
11Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
12-------------------------------------------------------------------------- */
13// END SHORT COPYRIGHT
14
15
16/*
17Old simple vector example now used just for validation testing
18*/
19// BEGIN PROGRAM
20
21// need this to define size_t
22# include <cstddef>
23
24// # include <vector>
25// # define MySimpleVector std::vector
26
27// # include <valarray>
28// # define MySimpleVector std::valarray
29
30// Assuming CppAD (http://www.seanet.com/~bradbell/cppad) is installed
31# include <cppad/vector.hpp>
32# define MySimpleVector CppAD::vector
33
34// Assuming Boost (http://www.boost.org) is installed
35// # include <boost/numeric/ublas/vector.hpp>
36// # define MySimpleVector boost::numeric::ublas::vector
37
38// A special type of element that counts assignment operations
39class MyInt {
40public:
41        // constructors
42        MyInt(void)       : count(0), value(0)
43        { }
44        MyInt(int _value) : count(0), value(_value)
45        { }
46        // assignment operator
47        MyInt& operator=(const MyInt &x)
48        {       value = x.value;
49                count++; 
50                return *this;
51        }
52        // equality operator
53        bool operator==(const MyInt &x) const
54        {       return value == x.value; }
55
56        int Value(void) const
57        {       return value; }
58        size_t Count(void) const
59        {       return count; }
60private:
61        size_t count;  // count number of assignments
62        int    value;  // value of this object
63};
64
65// Test of a Simple Vector template class
66bool SimpleVector(void)
67{       bool ok = true;
68        typedef MySimpleVector<MyInt> vector;    // class we are testing
69        typedef vector::value_type myInt;        // type of elements
70
71        vector x;                // default constructor
72        ok &= (x.size() == 0);
73
74        x.resize(2);             // resize and set element assignment
75        ok &= (x.size() == 2);
76        x[0] = myInt(0);
77        x[1] = myInt(1);
78
79        vector y(2);             // sizing constructor
80        ok &= (y.size() == 2);
81
82        const vector z(x);       // copy constructor and const element access
83        ok &= (z.size() == 2);
84        ok &= ( (z[0] == myInt(0)) && (z[1] == myInt(1)) );
85
86        // check that vector assignment
87        x[0] = 2;                      // modify so that assignment changes x
88        size_t x0count = x[0].Count(); // store initial counts
89        size_t y1count = y[1].Count();
90        size_t z0count = z[0].Count();
91        x = y = z;                     // vector assignment
92
93        // check resulting values
94        ok &= ( (x[0] == myInt(0)) && (x[1] == myInt(1)) );
95        ok &= ( (y[0] == myInt(0)) && (y[1] == myInt(1)) );
96        ok &= ( (z[0] == myInt(0)) && (z[1] == myInt(1)) );
97
98        // check that MyInt assignment was called (not raw memory copy)
99        ok &= (x[0].Count() == x0count + 1);
100        ok &= (y[1].Count() == y1count + 1);
101        ok &= (z[0].Count() == z0count);
102
103        return ok;
104}
105
106// END PROGRAM
Note: See TracBrowser for help on using the repository browser.