| 44 | } |
| 45 | |
| 46 | bool CppAD_vector(void) |
| 47 | { bool ok = true; |
| 48 | using CppAD::vector; // so can use vector instead of CppAD::vector |
| 49 | typedef double Scalar; // change double to test other types |
| 50 | |
| 51 | // check Simple Vector specifications |
| 52 | CppAD::CheckSimpleVector< Scalar, vector<Scalar> >(); |
| 53 | |
| 54 | // check constructor with size_t, with int, and with value |
| 55 | size_t two_s = 2; |
| 56 | int two_i = 2; |
| 57 | Scalar value = 5.0; |
| 58 | vector<Scalar> vec(2), other(two_s), another(two_i, value); |
| 59 | ok &= another[0] == 5.0; |
| 60 | ok &= another[1] == 5.0; |
| 61 | |
| 62 | // check resize with size_t and with int |
| 63 | vec.resize(2); |
| 64 | other.resize(two_s); |
| 65 | another.resize(two_i); |
| 66 | |
| 67 | // assignment returns reference for use in other assignments |
| 68 | another[0] = Scalar(1); |
| 69 | another[1] = Scalar(2); |
| 70 | vec = other = another; |
| 71 | for(size_t i = 0; i < 2; ++i) |
| 72 | { ok &= vec[i] == other[i]; |
| 73 | ok &= vec[i] == another[i]; |
| 74 | } |
| 75 | |
| 76 | // operator == |
| 77 | ok &= vec == other; |
| 78 | ok &= other == another; |
| 79 | |
| 80 | // initializer constructor |
| 81 | vector<Scalar> yet_another = { 1.0, 3.0}; |
| 82 | ok &= yet_another.size() == 2; |
| 83 | ok &= yet_another[0] == 1.0; |
| 84 | ok &= yet_another[1] == 3.0; |
| 85 | |
| 86 | // operator <=, >= |
| 87 | ok &= vec <= yet_another; |
| 88 | ok &= yet_another >= vec; |
| 89 | |
| 90 | // test of output |
| 91 | std::string correct= "{ 1, 2 }"; |
| 92 | std::string str; |
| 93 | std::ostringstream buf; |
| 94 | buf << vec; |
| 95 | str = buf.str(); |
| 96 | ok &= (str == correct); |
| 97 | |
| 98 | // swap |
| 99 | other[0] = vec[0] + 1; |
| 100 | vec.swap(other); |
| 101 | ok &= vec[0] == other[0] + 1; |
| 102 | |
| 103 | // clear |