|
| 1 | +/* |
| 2 | + Copyright 2005-2013 Adobe Systems Incorporated |
| 3 | + Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt |
| 4 | + or a copy at http://stlab.adobe.com/licenses.html) |
| 5 | +*/ |
| 6 | + |
| 7 | +/******************************************************************************/ |
| 8 | + |
| 9 | +// asl |
| 10 | +#include <adobe/arg_stream.hpp> |
| 11 | + |
| 12 | +#include <adobe/config.hpp> |
| 13 | + |
| 14 | +// boost |
| 15 | +#define BOOST_TEST_MAIN |
| 16 | +#include <boost/test/unit_test.hpp> |
| 17 | + |
| 18 | +// std |
| 19 | +#include <string> |
| 20 | + |
| 21 | +BOOST_AUTO_TEST_CASE(arg_stream_single_basic_functionality) { |
| 22 | + // Test basic construction and single use |
| 23 | + adobe::arg_stream::single<int> stream(42); |
| 24 | + |
| 25 | + // Should not be at EOF initially |
| 26 | + BOOST_CHECK(!stream.eof()); |
| 27 | + |
| 28 | + // Get the value once |
| 29 | + int value = stream.get_next_arg<int>(); |
| 30 | + BOOST_CHECK_EQUAL(value, 42); |
| 31 | + |
| 32 | + // Should be at EOF after single use |
| 33 | + BOOST_CHECK(stream.eof()); |
| 34 | + |
| 35 | + // Should throw no_more_args if we try to get another arg |
| 36 | + BOOST_CHECK_THROW(stream.get_next_arg<int>(), adobe::arg_stream::no_more_args); |
| 37 | +} |
| 38 | + |
| 39 | +BOOST_AUTO_TEST_CASE(arg_stream_single_multiple_count) { |
| 40 | + // Test with repeat count |
| 41 | + adobe::arg_stream::single<std::string> stream("hello", 3); |
| 42 | + |
| 43 | + // Should not be at EOF initially |
| 44 | + BOOST_CHECK(!stream.eof()); |
| 45 | + |
| 46 | + // Get the value three times |
| 47 | + for (int i = 0; i < 3; ++i) { |
| 48 | + BOOST_CHECK(!stream.eof()); |
| 49 | + std::string value = stream.get_next_arg<std::string>(); |
| 50 | + BOOST_CHECK_EQUAL(value, "hello"); |
| 51 | + } |
| 52 | + |
| 53 | + // Should be at EOF after three uses |
| 54 | + BOOST_CHECK(stream.eof()); |
| 55 | + |
| 56 | + // Should throw no_more_args if we try to get another arg |
| 57 | + BOOST_CHECK_THROW(stream.get_next_arg<std::string>(), adobe::arg_stream::no_more_args); |
| 58 | +} |
| 59 | + |
| 60 | +BOOST_AUTO_TEST_CASE(arg_stream_single_type_conversion) { |
| 61 | + // Test implicit type conversions |
| 62 | + adobe::arg_stream::single<int> stream(42); |
| 63 | + |
| 64 | + // Convert int to double |
| 65 | + double d_value = stream.get_next_arg<double>(); |
| 66 | + BOOST_CHECK_EQUAL(d_value, 42.0); |
| 67 | +} |
| 68 | + |
| 69 | +BOOST_AUTO_TEST_CASE(arg_stream_single_zero_count) { |
| 70 | + // Test with zero repeat count |
| 71 | + adobe::arg_stream::single<int> stream(42, 0); |
| 72 | + |
| 73 | + // Should be at EOF immediately |
| 74 | + BOOST_CHECK(stream.eof()); |
| 75 | + |
| 76 | + // Should throw no_more_args immediately |
| 77 | + BOOST_CHECK_THROW(stream.get_next_arg<int>(), adobe::arg_stream::no_more_args); |
| 78 | +} |
| 79 | + |
| 80 | +BOOST_AUTO_TEST_CASE(arg_stream_single_copy_semantics) { |
| 81 | + // Test that the value is properly stored and accessed |
| 82 | + std::string original = "test_string"; |
| 83 | + adobe::arg_stream::single<std::string> stream(original); |
| 84 | + |
| 85 | + std::string retrieved = stream.get_next_arg<std::string>(); |
| 86 | + BOOST_CHECK_EQUAL(retrieved, original); |
| 87 | + |
| 88 | + // Original should be unchanged |
| 89 | + BOOST_CHECK_EQUAL(original, "test_string"); |
| 90 | +} |
| 91 | + |
| 92 | +BOOST_AUTO_TEST_CASE(arg_stream_single_reference_type) { |
| 93 | + // Test with reference types |
| 94 | + int original = 100; |
| 95 | + adobe::arg_stream::single<int> stream(original, 2); |
| 96 | + |
| 97 | + // Modify original |
| 98 | + original = 200; |
| 99 | + |
| 100 | + // Stream should still have the original value (100) since it stores by value |
| 101 | + int first = stream.get_next_arg<int>(); |
| 102 | + int second = stream.get_next_arg<int>(); |
| 103 | + BOOST_CHECK_EQUAL(first, 100); |
| 104 | + BOOST_CHECK_EQUAL(second, 100); |
| 105 | +} |
| 106 | + |
| 107 | +// Test functions for call tests |
| 108 | +int add_two_ints(int a, int b) { |
| 109 | + return a + b; |
| 110 | +} |
| 111 | + |
| 112 | +int add_three_ints(int a, int b, int c) { |
| 113 | + return a + b + c; |
| 114 | +} |
| 115 | + |
| 116 | +double multiply_double_int(double d, int i) { |
| 117 | + return d * i; |
| 118 | +} |
| 119 | + |
| 120 | +void void_function(int /* value */) { |
| 121 | + // Just consume the value, nothing to return |
| 122 | +} |
| 123 | + |
| 124 | +std::string concat_strings(const std::string& a, const std::string& b) { |
| 125 | + return a + b; |
| 126 | +} |
| 127 | + |
| 128 | +// Test class for member function tests |
| 129 | +class TestClass { |
| 130 | +public: |
| 131 | + int value; |
| 132 | + TestClass(int v) : value(v) {} |
| 133 | + |
| 134 | + int add_to_value(int x) { |
| 135 | + return value + x; |
| 136 | + } |
| 137 | + |
| 138 | + int multiply_values(int x, int y) { |
| 139 | + return value * x * y; |
| 140 | + } |
| 141 | +}; |
| 142 | + |
| 143 | +BOOST_AUTO_TEST_CASE(arg_stream_call_basic_function) { |
| 144 | + // Test calling a simple function with two arguments |
| 145 | + adobe::arg_stream::single<int> stream1(10, 2); |
| 146 | + |
| 147 | + int result = adobe::arg_stream::call(add_two_ints, stream1); |
| 148 | + BOOST_CHECK_EQUAL(result, 20); // 10 + 10 |
| 149 | +} |
| 150 | + |
| 151 | +BOOST_AUTO_TEST_CASE(arg_stream_call_mixed_types) { |
| 152 | + // Test calling a function with mixed argument types |
| 153 | + adobe::arg_stream::single<double> double_stream(2.5); |
| 154 | + adobe::arg_stream::single<int> int_stream(4); |
| 155 | + |
| 156 | + auto chained = adobe::arg_stream::make_chain(double_stream, int_stream); |
| 157 | + |
| 158 | + double result = adobe::arg_stream::call(multiply_double_int, chained); |
| 159 | + BOOST_CHECK_EQUAL(result, 10.0); // 2.5 * 4 |
| 160 | +} |
| 161 | + |
| 162 | +BOOST_AUTO_TEST_CASE(arg_stream_call_void_function) { |
| 163 | + // Test calling a void function |
| 164 | + adobe::arg_stream::single<int> stream(42); |
| 165 | + |
| 166 | + // Should not throw and should consume the argument |
| 167 | + adobe::arg_stream::call(void_function, stream); |
| 168 | + |
| 169 | + // Stream should be exhausted |
| 170 | + BOOST_CHECK(stream.eof()); |
| 171 | +} |
| 172 | + |
| 173 | +BOOST_AUTO_TEST_CASE(arg_stream_call_string_function) { |
| 174 | + // Test calling a function with string arguments |
| 175 | + adobe::arg_stream::single<std::string> stream1("Hello"); |
| 176 | + adobe::arg_stream::single<std::string> stream2(" World"); |
| 177 | + |
| 178 | + auto chained = adobe::arg_stream::make_chain(stream1, stream2); |
| 179 | + |
| 180 | + std::string result = adobe::arg_stream::call(concat_strings, chained); |
| 181 | + BOOST_CHECK_EQUAL(result, "Hello World"); |
| 182 | +} |
| 183 | + |
| 184 | +BOOST_AUTO_TEST_CASE(arg_stream_call_member_function) { |
| 185 | + // Test calling member functions |
| 186 | + TestClass obj(5); |
| 187 | + adobe::arg_stream::single<int> stream(10); |
| 188 | + |
| 189 | + int result = adobe::arg_stream::call(&obj, &TestClass::add_to_value, stream); |
| 190 | + BOOST_CHECK_EQUAL(result, 15); // 5 + 10 |
| 191 | +} |
| 192 | + |
| 193 | +BOOST_AUTO_TEST_CASE(arg_stream_call_member_function_multiple_args) { |
| 194 | + // Test calling member function with multiple arguments |
| 195 | + TestClass obj(2); |
| 196 | + adobe::arg_stream::single<int> stream1(3); |
| 197 | + adobe::arg_stream::single<int> stream2(4); |
| 198 | + |
| 199 | + auto chained = adobe::arg_stream::make_chain(stream1, stream2); |
| 200 | + |
| 201 | + int result = adobe::arg_stream::call(&obj, &TestClass::multiply_values, chained); |
| 202 | + BOOST_CHECK_EQUAL(result, 24); // 2 * 3 * 4 |
| 203 | +} |
| 204 | + |
| 205 | +BOOST_AUTO_TEST_CASE(arg_stream_call_insufficient_args) { |
| 206 | + // Test that call throws when there are insufficient arguments |
| 207 | + adobe::arg_stream::single<int> stream(10, 1); // Only one argument available |
| 208 | + |
| 209 | + // Should throw no_more_args when trying to call a function requiring two arguments |
| 210 | + BOOST_CHECK_THROW(adobe::arg_stream::call(add_two_ints, stream), adobe::arg_stream::no_more_args); |
| 211 | +} |
0 commit comments