test/test_buffer.cpp

changeset 541
67e078518935
parent 539
9cd98da9ee17
child 542
45bcfd152f85
equal deleted inserted replaced
540:47e0f2237a94 541:67e078518935
34 static void expect_default_flush_config(CxBuffer *buf) { 34 static void expect_default_flush_config(CxBuffer *buf) {
35 EXPECT_EQ(buf->flush_blkmax, 0); 35 EXPECT_EQ(buf->flush_blkmax, 0);
36 EXPECT_EQ(buf->flush_blksize, 4096); 36 EXPECT_EQ(buf->flush_blksize, 4096);
37 EXPECT_EQ(buf->flush_threshold, SIZE_MAX); 37 EXPECT_EQ(buf->flush_threshold, SIZE_MAX);
38 EXPECT_EQ(buf->flush_func, nullptr); 38 EXPECT_EQ(buf->flush_func, nullptr);
39 EXPECT_EQ(buf->flush_target, nullptr);
39 } 40 }
40 41
41 TEST(BufferInit, WrapSpace) { 42 TEST(BufferInit, WrapSpace) {
42 CxTestingAllocator alloc; 43 CxTestingAllocator alloc;
43 CxBuffer buf; 44 CxBuffer buf;
309 310
310 TEST_F(BufferWrite, SizeOneFit) { 311 TEST_F(BufferWrite, SizeOneFit) {
311 const char *data = "test"; 312 const char *data = "test";
312 ASSERT_EQ(buf.capacity, 8); 313 ASSERT_EQ(buf.capacity, 8);
313 ASSERT_EQ(buf.pos, 4); 314 ASSERT_EQ(buf.pos, 4);
315 ASSERT_EQ(buf.size, 4);
314 size_t written = cxBufferWrite(data, 1, 4, &buf); 316 size_t written = cxBufferWrite(data, 1, 4, &buf);
315 EXPECT_EQ(written, 4); 317 EXPECT_EQ(written, 4);
316 EXPECT_EQ(buf.size, 8); 318 EXPECT_EQ(buf.size, 8);
317 EXPECT_EQ(buf.pos, 8); 319 EXPECT_EQ(buf.pos, 8);
318 EXPECT_EQ(buf.capacity, 8); 320 EXPECT_EQ(buf.capacity, 8);
319 EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0); 321 EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0);
320 } 322 }
321 323
322 TEST_F(BufferWrite, SizeOneOverwrite) {
323
324 }
325
326 TEST_F(BufferWrite, SizeOnePartialOverwrite) {
327
328 }
329
330 TEST_F(BufferWrite, SizeOneDiscard) { 324 TEST_F(BufferWrite, SizeOneDiscard) {
331 const char *data = "testing"; 325 const char *data = "testing";
332 ASSERT_EQ(buf.capacity, 8); 326 ASSERT_EQ(buf.capacity, 8);
333 ASSERT_EQ(buf.pos, 4); 327 ASSERT_EQ(buf.pos, 4);
328 ASSERT_EQ(buf.size, 4);
334 size_t written = cxBufferWrite(data, 1, 7, &buf); 329 size_t written = cxBufferWrite(data, 1, 7, &buf);
335 EXPECT_EQ(written, 4); 330 EXPECT_EQ(written, 4);
336 EXPECT_EQ(buf.size, 8); 331 EXPECT_EQ(buf.size, 8);
337 EXPECT_EQ(buf.pos, 8); 332 EXPECT_EQ(buf.pos, 8);
338 EXPECT_EQ(buf.capacity, 8); 333 EXPECT_EQ(buf.capacity, 8);
342 TEST_F(BufferWrite, SizeOneExtend) { 337 TEST_F(BufferWrite, SizeOneExtend) {
343 buf.flags |= CX_BUFFER_AUTO_EXTEND; 338 buf.flags |= CX_BUFFER_AUTO_EXTEND;
344 const char *data = "testing"; 339 const char *data = "testing";
345 ASSERT_EQ(buf.capacity, 8); 340 ASSERT_EQ(buf.capacity, 8);
346 ASSERT_EQ(buf.pos, 4); 341 ASSERT_EQ(buf.pos, 4);
342 ASSERT_EQ(buf.size, 4);
347 size_t written = cxBufferWrite(data, 1, 7, &buf); 343 size_t written = cxBufferWrite(data, 1, 7, &buf);
348 EXPECT_EQ(written, 7); 344 EXPECT_EQ(written, 7);
349 EXPECT_EQ(buf.size, 11); 345 EXPECT_EQ(buf.size, 11);
350 EXPECT_EQ(buf.pos, 11); 346 EXPECT_EQ(buf.pos, 11);
351 EXPECT_GE(buf.capacity, 11); 347 EXPECT_GE(buf.capacity, 11);
352 EXPECT_EQ(memcmp(buf.space, "preptesting", 11), 0); 348 EXPECT_EQ(memcmp(buf.space, "preptesting", 11), 0);
353 } 349 }
354 350
355 TEST_F(BufferWrite, MultibyteOverwrite) {
356
357 }
358
359 TEST_F(BufferWrite, MultibytePartialOverwrite) {
360
361 }
362
363 TEST_F(BufferWrite, MultibyteFit) { 351 TEST_F(BufferWrite, MultibyteFit) {
364 352 const char *data = "test";
353 ASSERT_EQ(buf.capacity, 8);
354 ASSERT_EQ(buf.pos, 4);
355 ASSERT_EQ(buf.size, 4);
356 size_t written = cxBufferWrite(data, 2, 2, &buf);
357 EXPECT_EQ(written, 2);
358 EXPECT_EQ(buf.size, 8);
359 EXPECT_EQ(buf.pos, 8);
360 EXPECT_EQ(buf.capacity, 8);
361 EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0);
365 } 362 }
366 363
367 TEST_F(BufferWrite, MultibyteDiscard) { 364 TEST_F(BufferWrite, MultibyteDiscard) {
365 const char *data = "tester";
366 ASSERT_EQ(buf.capacity, 8);
367 ASSERT_EQ(buf.size, 4);
368 buf.pos = 3;
369 size_t written = cxBufferWrite(data, 2, 3, &buf);
368 // remember: whole elements are discarded if they do not fit 370 // remember: whole elements are discarded if they do not fit
371 EXPECT_EQ(written, 2);
372 EXPECT_EQ(buf.size, 7);
373 EXPECT_EQ(buf.pos, 7);
374 EXPECT_EQ(buf.capacity, 8);
375 EXPECT_EQ(memcmp(buf.space, "pretest\0", 8), 0);
369 } 376 }
370 377
371 TEST_F(BufferWrite, MultibyteExtend) { 378 TEST_F(BufferWrite, MultibyteExtend) {
372 379 buf.flags |= CX_BUFFER_AUTO_EXTEND;
380 const char *data = "tester";
381 ASSERT_EQ(buf.capacity, 8);
382 ASSERT_EQ(buf.size, 4);
383 buf.pos = 3;
384 size_t written = cxBufferWrite(data, 2, 3, &buf);
385 // remember: whole elements are discarded if they do not fit
386 EXPECT_EQ(written, 3);
387 EXPECT_EQ(buf.size, 9);
388 EXPECT_EQ(buf.pos, 9);
389 EXPECT_GE(buf.capacity, 9);
390 EXPECT_EQ(memcmp(buf.space, "pretester\0", 10), 0);
373 } 391 }
374 392
375 TEST_F(BufferWrite, PutcWrapperFit) { 393 TEST_F(BufferWrite, PutcWrapperFit) {
376 394 ASSERT_EQ(buf.capacity, 8);
377 } 395 ASSERT_EQ(buf.pos, 4);
378 396 ASSERT_EQ(buf.size, 4);
379 TEST_F(BufferWrite, PutcWrapperOverwrite) { 397 int c = cxBufferPut(&buf, 0x20 | 'a');
380 398 EXPECT_EQ(c, 'a');
381 } 399 EXPECT_EQ(buf.size, 5);
382 400 EXPECT_EQ(buf.pos, 5);
383 TEST_F(BufferWrite, PutcWrapperPartialOverwrite) { 401 EXPECT_EQ(buf.capacity, 8);
384 402 EXPECT_EQ(memcmp(buf.space, "prepa\0", 6), 0);
385 } 403 }
386 404
387 TEST_F(BufferWrite, PutcWrapperDiscard) { 405 TEST_F(BufferWrite, PutcWrapperDiscard) {
388 406 ASSERT_EQ(buf.capacity, 8);
407 ASSERT_EQ(buf.size, 4);
408 buf.pos = 8;
409 int c = cxBufferPut(&buf, 0x20 | 'a');
410 EXPECT_EQ(c, EOF);
411 EXPECT_EQ(buf.size, 4);
412 EXPECT_EQ(buf.pos, 8);
413 EXPECT_EQ(buf.capacity, 8);
414 EXPECT_EQ(memcmp(buf.space, "prep\0\0\0\0\0", 9), 0);
389 } 415 }
390 416
391 TEST_F(BufferWrite, PutcWrapperExtend) { 417 TEST_F(BufferWrite, PutcWrapperExtend) {
392 418 buf.flags |= CX_BUFFER_AUTO_EXTEND;
419 ASSERT_EQ(buf.capacity, 8);
420 ASSERT_EQ(buf.size, 4);
421 buf.pos = 8;
422 int c = cxBufferPut(&buf, 0x20 | 'a');
423 EXPECT_EQ(c, 'a');
424 EXPECT_EQ(buf.size, 9);
425 EXPECT_EQ(buf.pos, 9);
426 EXPECT_GE(buf.capacity, 9);
427 EXPECT_EQ(memcmp(buf.space, "prep\0\0\0\0a\0", 10), 0);
393 } 428 }
394 429
395 TEST_F(BufferWrite, PutStringWrapperFit) { 430 TEST_F(BufferWrite, PutStringWrapperFit) {
396 431 const char *data = "test";
397 } 432 ASSERT_EQ(buf.capacity, 8);
398 433 ASSERT_EQ(buf.pos, 4);
399 TEST_F(BufferWrite, PutStringWrapperOverwrite) { 434 ASSERT_EQ(buf.size, 4);
400 435 size_t written = cxBufferPutString(&buf, data);
401 } 436 EXPECT_EQ(written, 4);
402 437 EXPECT_EQ(buf.size, 8);
403 TEST_F(BufferWrite, PutStringWrapperPartialOverwrite) { 438 EXPECT_EQ(buf.pos, 8);
404 439 EXPECT_EQ(buf.capacity, 8);
440 EXPECT_EQ(memcmp(buf.space, "preptest", 8), 0);
405 } 441 }
406 442
407 TEST_F(BufferWrite, PutStringWrapperDiscard) { 443 TEST_F(BufferWrite, PutStringWrapperDiscard) {
408 444 const char *data = "testing";
445 ASSERT_EQ(buf.capacity, 8);
446 ASSERT_EQ(buf.pos, 4);
447 ASSERT_EQ(buf.size, 4);
448 size_t written = cxBufferPutString(&buf, data);
449 EXPECT_EQ(written, 4);
450 EXPECT_EQ(buf.size, 8);
451 EXPECT_EQ(buf.pos, 8);
452 EXPECT_EQ(buf.capacity, 8);
453 EXPECT_EQ(memcmp(buf.space, "preptest\0", 9), 0);
409 } 454 }
410 455
411 TEST_F(BufferWrite, PutStringWrapperExtend) { 456 TEST_F(BufferWrite, PutStringWrapperExtend) {
412 457 buf.flags |= CX_BUFFER_AUTO_EXTEND;
413 } 458 const char *data = "testing";
459 ASSERT_EQ(buf.capacity, 8);
460 ASSERT_EQ(buf.pos, 4);
461 ASSERT_EQ(buf.size, 4);
462 size_t written = cxBufferPutString(&buf, data);
463 EXPECT_EQ(written, 7);
464 EXPECT_EQ(buf.size, 11);
465 EXPECT_EQ(buf.pos, 11);
466 EXPECT_GE(buf.capacity, 11);
467 EXPECT_EQ(memcmp(buf.space, "preptesting", 11), 0);
468 }
469
470 TEST_F(BufferWrite, MultOverflow) {
471 const char *data = "testing";
472 ASSERT_EQ(buf.capacity, 8);
473 ASSERT_EQ(buf.pos, 4);
474 ASSERT_EQ(buf.size, 4);
475 size_t written = cxBufferWrite(data, 8, SIZE_MAX / 4, &buf);
476 EXPECT_EQ(written, 0);
477 EXPECT_EQ(buf.capacity, 8);
478 EXPECT_EQ(buf.pos, 4);
479 EXPECT_EQ(buf.size, 4);
480 EXPECT_EQ(memcmp(buf.space, "prep\0", 5), 0);
481 }
482
483 TEST_F(BufferWrite, MaxCapaOverflow) {
484 buf.flags |= CX_BUFFER_AUTO_EXTEND;
485 const char *data = "testing";
486 ASSERT_EQ(buf.capacity, 8);
487 ASSERT_EQ(buf.pos, 4);
488 ASSERT_EQ(buf.size, 4);
489 size_t written = cxBufferWrite(data, 1, SIZE_MAX - 2, &buf);
490 EXPECT_EQ(written, 0);
491 EXPECT_EQ(buf.capacity, 8);
492 EXPECT_EQ(buf.pos, 4);
493 EXPECT_EQ(buf.size, 4);
494 EXPECT_EQ(memcmp(buf.space, "prep\0", 5), 0);
495 }
496
497 TEST_F(BufferWrite, OnlyOverwrite) {
498 buf.flags |= CX_BUFFER_AUTO_EXTEND;
499 ASSERT_EQ(buf.capacity, 8);
500 memcpy(buf.space, "preptest", 8);
501 buf.pos = 3;
502 buf.size = 8;
503 size_t written = cxBufferWrite("XXX", 2, 2, &buf);
504 EXPECT_EQ(written, 2);
505 EXPECT_EQ(buf.capacity, 8);
506 EXPECT_EQ(buf.size, 8);
507 EXPECT_EQ(buf.pos, 7);
508 EXPECT_EQ(memcmp(buf.space, "preXXX\0t", 8), 0);
509 }

mercurial