| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2009, Google Inc. | 2 * Copyright 2009, Google Inc. |
| 3 * All rights reserved. | 3 * All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 CommandHeader header; | 108 CommandHeader header; |
| 109 | 109 |
| 110 // add a single command, no args | 110 // add a single command, no args |
| 111 header.size = 1; | 111 header.size = 1; |
| 112 header.command = 123; | 112 header.command = 123; |
| 113 buffer()[put++].value_header = header; | 113 buffer()[put++].value_header = header; |
| 114 | 114 |
| 115 parser->set_put(put); | 115 parser->set_put(put); |
| 116 EXPECT_EQ(put, parser->put()); | 116 EXPECT_EQ(put, parser->put()); |
| 117 | 117 |
| 118 AddDoCommandExpect(BufferSyncInterface::PARSE_NO_ERROR, 123, 0, NULL); | 118 AddDoCommandExpect(BufferSyncInterface::kParseNoError, 123, 0, NULL); |
| 119 EXPECT_EQ(BufferSyncInterface::PARSE_NO_ERROR, parser->ProcessCommand()); | 119 EXPECT_EQ(BufferSyncInterface::kParseNoError, parser->ProcessCommand()); |
| 120 EXPECT_EQ(put, parser->get()); | 120 EXPECT_EQ(put, parser->get()); |
| 121 Mock::VerifyAndClearExpectations(api_mock()); | 121 Mock::VerifyAndClearExpectations(api_mock()); |
| 122 | 122 |
| 123 // add a single command, 2 args | 123 // add a single command, 2 args |
| 124 header.size = 3; | 124 header.size = 3; |
| 125 header.command = 456; | 125 header.command = 456; |
| 126 buffer()[put++].value_header = header; | 126 buffer()[put++].value_header = header; |
| 127 buffer()[put++].value_int32 = 2134; | 127 buffer()[put++].value_int32 = 2134; |
| 128 buffer()[put++].value_float = 1.f; | 128 buffer()[put++].value_float = 1.f; |
| 129 | 129 |
| 130 parser->set_put(put); | 130 parser->set_put(put); |
| 131 EXPECT_EQ(put, parser->put()); | 131 EXPECT_EQ(put, parser->put()); |
| 132 | 132 |
| 133 CommandBufferEntry param_array[2]; | 133 CommandBufferEntry param_array[2]; |
| 134 param_array[0].value_int32 = 2134; | 134 param_array[0].value_int32 = 2134; |
| 135 param_array[1].value_float = 1.f; | 135 param_array[1].value_float = 1.f; |
| 136 AddDoCommandExpect(BufferSyncInterface::PARSE_NO_ERROR, 456, 2, param_array); | 136 AddDoCommandExpect(BufferSyncInterface::kParseNoError, 456, 2, param_array); |
| 137 EXPECT_EQ(BufferSyncInterface::PARSE_NO_ERROR, parser->ProcessCommand()); | 137 EXPECT_EQ(BufferSyncInterface::kParseNoError, parser->ProcessCommand()); |
| 138 EXPECT_EQ(put, parser->get()); | 138 EXPECT_EQ(put, parser->get()); |
| 139 Mock::VerifyAndClearExpectations(api_mock()); | 139 Mock::VerifyAndClearExpectations(api_mock()); |
| 140 } | 140 } |
| 141 | 141 |
| 142 // Tests having multiple commands in the buffer. | 142 // Tests having multiple commands in the buffer. |
| 143 TEST_F(CommandParserTest, TestMultipleCommands) { | 143 TEST_F(CommandParserTest, TestMultipleCommands) { |
| 144 scoped_ptr<CommandParser> parser(MakeParser(10)); | 144 scoped_ptr<CommandParser> parser(MakeParser(10)); |
| 145 CommandBufferOffset put = parser->put(); | 145 CommandBufferOffset put = parser->put(); |
| 146 CommandHeader header; | 146 CommandHeader header; |
| 147 | 147 |
| 148 // add 2 commands, test with single ProcessCommand() | 148 // add 2 commands, test with single ProcessCommand() |
| 149 header.size = 2; | 149 header.size = 2; |
| 150 header.command = 789; | 150 header.command = 789; |
| 151 buffer()[put++].value_header = header; | 151 buffer()[put++].value_header = header; |
| 152 buffer()[put++].value_int32 = 5151; | 152 buffer()[put++].value_int32 = 5151; |
| 153 | 153 |
| 154 CommandBufferOffset put_cmd2 = put; | 154 CommandBufferOffset put_cmd2 = put; |
| 155 header.size = 2; | 155 header.size = 2; |
| 156 header.command = 2121; | 156 header.command = 2121; |
| 157 buffer()[put++].value_header = header; | 157 buffer()[put++].value_header = header; |
| 158 buffer()[put++].value_int32 = 3434; | 158 buffer()[put++].value_int32 = 3434; |
| 159 | 159 |
| 160 parser->set_put(put); | 160 parser->set_put(put); |
| 161 EXPECT_EQ(put, parser->put()); | 161 EXPECT_EQ(put, parser->put()); |
| 162 | 162 |
| 163 CommandBufferEntry param_array[2]; | 163 CommandBufferEntry param_array[2]; |
| 164 param_array[0].value_int32 = 5151; | 164 param_array[0].value_int32 = 5151; |
| 165 AddDoCommandExpect(BufferSyncInterface::PARSE_NO_ERROR, 789, 1, param_array); | 165 AddDoCommandExpect(BufferSyncInterface::kParseNoError, 789, 1, param_array); |
| 166 param_array[1].value_int32 = 3434; | 166 param_array[1].value_int32 = 3434; |
| 167 AddDoCommandExpect(BufferSyncInterface::PARSE_NO_ERROR, 2121, 1, | 167 AddDoCommandExpect(BufferSyncInterface::kParseNoError, 2121, 1, |
| 168 param_array+1); | 168 param_array+1); |
| 169 | 169 |
| 170 EXPECT_EQ(BufferSyncInterface::PARSE_NO_ERROR, parser->ProcessCommand()); | 170 EXPECT_EQ(BufferSyncInterface::kParseNoError, parser->ProcessCommand()); |
| 171 EXPECT_EQ(put_cmd2, parser->get()); | 171 EXPECT_EQ(put_cmd2, parser->get()); |
| 172 EXPECT_EQ(BufferSyncInterface::PARSE_NO_ERROR, parser->ProcessCommand()); | 172 EXPECT_EQ(BufferSyncInterface::kParseNoError, parser->ProcessCommand()); |
| 173 EXPECT_EQ(put, parser->get()); | 173 EXPECT_EQ(put, parser->get()); |
| 174 Mock::VerifyAndClearExpectations(api_mock()); | 174 Mock::VerifyAndClearExpectations(api_mock()); |
| 175 | 175 |
| 176 // add 2 commands again, test with ProcessAllCommands() | 176 // add 2 commands again, test with ProcessAllCommands() |
| 177 header.size = 2; | 177 header.size = 2; |
| 178 header.command = 4545; | 178 header.command = 4545; |
| 179 buffer()[put++].value_header = header; | 179 buffer()[put++].value_header = header; |
| 180 buffer()[put++].value_int32 = 5656; | 180 buffer()[put++].value_int32 = 5656; |
| 181 | 181 |
| 182 header.size = 2; | 182 header.size = 2; |
| 183 header.command = 6767; | 183 header.command = 6767; |
| 184 buffer()[put++].value_header = header; | 184 buffer()[put++].value_header = header; |
| 185 buffer()[put++].value_int32 = 7878; | 185 buffer()[put++].value_int32 = 7878; |
| 186 | 186 |
| 187 parser->set_put(put); | 187 parser->set_put(put); |
| 188 EXPECT_EQ(put, parser->put()); | 188 EXPECT_EQ(put, parser->put()); |
| 189 | 189 |
| 190 param_array[0].value_int32 = 5656; | 190 param_array[0].value_int32 = 5656; |
| 191 AddDoCommandExpect(BufferSyncInterface::PARSE_NO_ERROR, 4545, 1, param_array); | 191 AddDoCommandExpect(BufferSyncInterface::kParseNoError, 4545, 1, param_array); |
| 192 param_array[1].value_int32 = 7878; | 192 param_array[1].value_int32 = 7878; |
| 193 AddDoCommandExpect(BufferSyncInterface::PARSE_NO_ERROR, 6767, 1, | 193 AddDoCommandExpect(BufferSyncInterface::kParseNoError, 6767, 1, |
| 194 param_array+1); | 194 param_array+1); |
| 195 | 195 |
| 196 EXPECT_EQ(BufferSyncInterface::PARSE_NO_ERROR, parser->ProcessAllCommands()); | 196 EXPECT_EQ(BufferSyncInterface::kParseNoError, parser->ProcessAllCommands()); |
| 197 EXPECT_EQ(put, parser->get()); | 197 EXPECT_EQ(put, parser->get()); |
| 198 Mock::VerifyAndClearExpectations(api_mock()); | 198 Mock::VerifyAndClearExpectations(api_mock()); |
| 199 } | 199 } |
| 200 | 200 |
| 201 // Tests that the parser will wrap correctly at the end of the buffer. | 201 // Tests that the parser will wrap correctly at the end of the buffer. |
| 202 TEST_F(CommandParserTest, TestWrap) { | 202 TEST_F(CommandParserTest, TestWrap) { |
| 203 scoped_ptr<CommandParser> parser(MakeParser(5)); | 203 scoped_ptr<CommandParser> parser(MakeParser(5)); |
| 204 CommandBufferOffset put = parser->put(); | 204 CommandBufferOffset put = parser->put(); |
| 205 CommandHeader header; | 205 CommandHeader header; |
| 206 | 206 |
| 207 // add 3 commands with no args (1 word each) | 207 // add 3 commands with no args (1 word each) |
| 208 for (unsigned int i = 0; i < 3; ++i) { | 208 for (unsigned int i = 0; i < 3; ++i) { |
| 209 header.size = 1; | 209 header.size = 1; |
| 210 header.command = i; | 210 header.command = i; |
| 211 buffer()[put++].value_header = header; | 211 buffer()[put++].value_header = header; |
| 212 AddDoCommandExpect(BufferSyncInterface::PARSE_NO_ERROR, i, 0, NULL); | 212 AddDoCommandExpect(BufferSyncInterface::kParseNoError, i, 0, NULL); |
| 213 } | 213 } |
| 214 parser->set_put(put); | 214 parser->set_put(put); |
| 215 EXPECT_EQ(put, parser->put()); | 215 EXPECT_EQ(put, parser->put()); |
| 216 EXPECT_EQ(BufferSyncInterface::PARSE_NO_ERROR, parser->ProcessAllCommands()); | 216 EXPECT_EQ(BufferSyncInterface::kParseNoError, parser->ProcessAllCommands()); |
| 217 EXPECT_EQ(put, parser->get()); | 217 EXPECT_EQ(put, parser->get()); |
| 218 Mock::VerifyAndClearExpectations(api_mock()); | 218 Mock::VerifyAndClearExpectations(api_mock()); |
| 219 | 219 |
| 220 // add 1 command with 1 arg (2 words). That should put us at the end of the | 220 // add 1 command with 1 arg (2 words). That should put us at the end of the |
| 221 // buffer. | 221 // buffer. |
| 222 header.size = 2; | 222 header.size = 2; |
| 223 header.command = 3; | 223 header.command = 3; |
| 224 buffer()[put++].value_header = header; | 224 buffer()[put++].value_header = header; |
| 225 buffer()[put++].value_int32 = 5; | 225 buffer()[put++].value_int32 = 5; |
| 226 CommandBufferEntry param; | 226 CommandBufferEntry param; |
| 227 param.value_int32 = 5; | 227 param.value_int32 = 5; |
| 228 AddDoCommandExpect(BufferSyncInterface::PARSE_NO_ERROR, 3, 1, ¶m); | 228 AddDoCommandExpect(BufferSyncInterface::kParseNoError, 3, 1, ¶m); |
| 229 | 229 |
| 230 DCHECK_EQ(5, put); | 230 DCHECK_EQ(5, put); |
| 231 put = 0; | 231 put = 0; |
| 232 parser->set_put(put); | 232 parser->set_put(put); |
| 233 EXPECT_EQ(put, parser->put()); | 233 EXPECT_EQ(put, parser->put()); |
| 234 EXPECT_EQ(BufferSyncInterface::PARSE_NO_ERROR, parser->ProcessAllCommands()); | 234 EXPECT_EQ(BufferSyncInterface::kParseNoError, parser->ProcessAllCommands()); |
| 235 EXPECT_EQ(put, parser->get()); | 235 EXPECT_EQ(put, parser->get()); |
| 236 Mock::VerifyAndClearExpectations(api_mock()); | 236 Mock::VerifyAndClearExpectations(api_mock()); |
| 237 | 237 |
| 238 // add 1 command with 1 arg (2 words). | 238 // add 1 command with 1 arg (2 words). |
| 239 header.size = 2; | 239 header.size = 2; |
| 240 header.command = 4; | 240 header.command = 4; |
| 241 buffer()[put++].value_header = header; | 241 buffer()[put++].value_header = header; |
| 242 buffer()[put++].value_int32 = 6; | 242 buffer()[put++].value_int32 = 6; |
| 243 param.value_int32 = 6; | 243 param.value_int32 = 6; |
| 244 AddDoCommandExpect(BufferSyncInterface::PARSE_NO_ERROR, 4, 1, ¶m); | 244 AddDoCommandExpect(BufferSyncInterface::kParseNoError, 4, 1, ¶m); |
| 245 parser->set_put(put); | 245 parser->set_put(put); |
| 246 EXPECT_EQ(put, parser->put()); | 246 EXPECT_EQ(put, parser->put()); |
| 247 EXPECT_EQ(BufferSyncInterface::PARSE_NO_ERROR, parser->ProcessAllCommands()); | 247 EXPECT_EQ(BufferSyncInterface::kParseNoError, parser->ProcessAllCommands()); |
| 248 EXPECT_EQ(put, parser->get()); | 248 EXPECT_EQ(put, parser->get()); |
| 249 Mock::VerifyAndClearExpectations(api_mock()); | 249 Mock::VerifyAndClearExpectations(api_mock()); |
| 250 } | 250 } |
| 251 | 251 |
| 252 // Tests error conditions. | 252 // Tests error conditions. |
| 253 TEST_F(CommandParserTest, TestError) { | 253 TEST_F(CommandParserTest, TestError) { |
| 254 scoped_ptr<CommandParser> parser(MakeParser(5)); | 254 scoped_ptr<CommandParser> parser(MakeParser(5)); |
| 255 CommandBufferOffset put = parser->put(); | 255 CommandBufferOffset put = parser->put(); |
| 256 CommandHeader header; | 256 CommandHeader header; |
| 257 | 257 |
| 258 // Generate a command with size 0. | 258 // Generate a command with size 0. |
| 259 header.size = 0; | 259 header.size = 0; |
| 260 header.command = 3; | 260 header.command = 3; |
| 261 buffer()[put++].value_header = header; | 261 buffer()[put++].value_header = header; |
| 262 | 262 |
| 263 parser->set_put(put); | 263 parser->set_put(put); |
| 264 EXPECT_EQ(put, parser->put()); | 264 EXPECT_EQ(put, parser->put()); |
| 265 EXPECT_EQ(BufferSyncInterface::PARSE_INVALID_SIZE, | 265 EXPECT_EQ(BufferSyncInterface::kParseInvalidSize, |
| 266 parser->ProcessAllCommands()); | 266 parser->ProcessAllCommands()); |
| 267 // check that no DoCommand call was made. | 267 // check that no DoCommand call was made. |
| 268 Mock::VerifyAndClearExpectations(api_mock()); | 268 Mock::VerifyAndClearExpectations(api_mock()); |
| 269 | 269 |
| 270 parser.reset(MakeParser(5)); | 270 parser.reset(MakeParser(5)); |
| 271 put = parser->put(); | 271 put = parser->put(); |
| 272 | 272 |
| 273 // Generate a command with size 6, extends beyond the end of the buffer. | 273 // Generate a command with size 6, extends beyond the end of the buffer. |
| 274 header.size = 6; | 274 header.size = 6; |
| 275 header.command = 3; | 275 header.command = 3; |
| 276 buffer()[put++].value_header = header; | 276 buffer()[put++].value_header = header; |
| 277 | 277 |
| 278 parser->set_put(put); | 278 parser->set_put(put); |
| 279 EXPECT_EQ(put, parser->put()); | 279 EXPECT_EQ(put, parser->put()); |
| 280 EXPECT_EQ(BufferSyncInterface::PARSE_OUT_OF_BOUNDS, | 280 EXPECT_EQ(BufferSyncInterface::kParseOutOfBounds, |
| 281 parser->ProcessAllCommands()); | 281 parser->ProcessAllCommands()); |
| 282 // check that no DoCommand call was made. | 282 // check that no DoCommand call was made. |
| 283 Mock::VerifyAndClearExpectations(api_mock()); | 283 Mock::VerifyAndClearExpectations(api_mock()); |
| 284 | 284 |
| 285 parser.reset(MakeParser(5)); | 285 parser.reset(MakeParser(5)); |
| 286 put = parser->put(); | 286 put = parser->put(); |
| 287 | 287 |
| 288 // Generates 2 commands. | 288 // Generates 2 commands. |
| 289 header.size = 1; | 289 header.size = 1; |
| 290 header.command = 3; | 290 header.command = 3; |
| 291 buffer()[put++].value_header = header; | 291 buffer()[put++].value_header = header; |
| 292 CommandBufferOffset put_post_fail = put; | 292 CommandBufferOffset put_post_fail = put; |
| 293 header.size = 1; | 293 header.size = 1; |
| 294 header.command = 4; | 294 header.command = 4; |
| 295 buffer()[put++].value_header = header; | 295 buffer()[put++].value_header = header; |
| 296 | 296 |
| 297 parser->set_put(put); | 297 parser->set_put(put); |
| 298 EXPECT_EQ(put, parser->put()); | 298 EXPECT_EQ(put, parser->put()); |
| 299 // have the first command fail to parse. | 299 // have the first command fail to parse. |
| 300 AddDoCommandExpect(BufferSyncInterface::PARSE_UNKNOWN_COMMAND, 3, 0, NULL); | 300 AddDoCommandExpect(BufferSyncInterface::kParseUnknownCommand, 3, 0, NULL); |
| 301 EXPECT_EQ(BufferSyncInterface::PARSE_UNKNOWN_COMMAND, | 301 EXPECT_EQ(BufferSyncInterface::kParseUnknownCommand, |
| 302 parser->ProcessAllCommands()); | 302 parser->ProcessAllCommands()); |
| 303 // check that only one command was executed, and that get reflects that | 303 // check that only one command was executed, and that get reflects that |
| 304 // correctly. | 304 // correctly. |
| 305 EXPECT_EQ(put_post_fail, parser->get()); | 305 EXPECT_EQ(put_post_fail, parser->get()); |
| 306 Mock::VerifyAndClearExpectations(api_mock()); | 306 Mock::VerifyAndClearExpectations(api_mock()); |
| 307 // make the second one succeed, and check that the parser recovered fine. | 307 // make the second one succeed, and check that the parser recovered fine. |
| 308 AddDoCommandExpect(BufferSyncInterface::PARSE_NO_ERROR, 4, 0, NULL); | 308 AddDoCommandExpect(BufferSyncInterface::kParseNoError, 4, 0, NULL); |
| 309 EXPECT_EQ(BufferSyncInterface::PARSE_NO_ERROR, parser->ProcessAllCommands()); | 309 EXPECT_EQ(BufferSyncInterface::kParseNoError, parser->ProcessAllCommands()); |
| 310 EXPECT_EQ(put, parser->get()); | 310 EXPECT_EQ(put, parser->get()); |
| 311 Mock::VerifyAndClearExpectations(api_mock()); | 311 Mock::VerifyAndClearExpectations(api_mock()); |
| 312 } | 312 } |
| 313 | 313 |
| 314 } // namespace command_buffer | 314 } // namespace command_buffer |
| 315 } // namespace o3d | 315 } // namespace o3d |
| OLD | NEW |