source: trunk/test_more/simple_vector.cpp @ 2506

Last change on this file since 2506 was 2506, checked in by bradbell, 8 years ago

Change Licenses: CPL-1.0 -> EPL-1.0, GPL-2.0->GPL-3.0

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