| 406 | } |
| 407 | |
| 408 | void SC::AsyncStreamsTest::createChildView() |
| 409 | { |
| 410 | constexpr size_t numberOfBuffers = 4; |
| 411 | AsyncBufferView buffers[numberOfBuffers]; |
| 412 | Buffer buffer; |
| 413 | SC_TEST_EXPECT(buffer.resizeWithoutInitializing(100)); |
| 414 | // Create parent buffer |
| 415 | buffers[0] = Span<char>(buffer.toSpan().data(), 100); |
| 416 | buffers[0].setReusable(true); |
| 417 | AsyncBuffersPool pool; |
| 418 | pool.setBuffers(buffers); |
| 419 | |
| 420 | // Get a buffer and fill it with data |
| 421 | AsyncBufferView::ID parentID; |
| 422 | Span<char> parentData; |
| 423 | SC_TEST_EXPECT(pool.requestNewBuffer(100, parentID, parentData)); |
| 424 | const char* testData = "Hello World! This is a test buffer for child views."; |
| 425 | memcpy(parentData.data(), testData, strlen(testData)); |
| 426 | |
| 427 | // Create child view |
| 428 | AsyncBufferView::ID childID; |
| 429 | SC_TEST_EXPECT(pool.createChildView(parentID, 6, 11, childID)); // "World" |
| 430 | |
| 431 | // Check child data |
| 432 | Span<const char> childData; |
| 433 | SC_TEST_EXPECT(pool.getReadableData(childID, childData)); |
| 434 | SC_TEST_EXPECT(childData.sizeInBytes() == 11); |
| 435 | SC_TEST_EXPECT(memcmp(childData.data(), "World! This ", 11) == 0); |
| 436 | |
| 437 | // Verify writable data on child (since parent is writable) |
| 438 | Span<char> childWritableData; |
| 439 | SC_TEST_EXPECT(pool.getWritableData(childID, childWritableData)); |
| 440 | SC_TEST_EXPECT(childWritableData.sizeInBytes() == 11); |
| 441 | childWritableData[0] = 'W'; // "World" |
| 442 | |
| 443 | // Create grandchild view (child of child) |
| 444 | AsyncBufferView::ID grandchildID; |
| 445 | SC_TEST_EXPECT( |
| 446 | pool.createChildView(childID, 7, 4, grandchildID)); // "This" (relative to child: 7+6=13 relative to parent) |
| 447 | |
| 448 | Span<const char> grandchildData; |
| 449 | SC_TEST_EXPECT(pool.getReadableData(grandchildID, grandchildData)); |
| 450 | SC_TEST_EXPECT(grandchildData.sizeInBytes() == 4); |
| 451 | SC_TEST_EXPECT(memcmp(grandchildData.data(), "This", 4) == 0); |
| 452 | |
| 453 | // Test error cases |
| 454 | AsyncBufferView::ID invalidID; |
| 455 | SC_TEST_EXPECT(not pool.createChildView(AsyncBufferView::ID(999), 0, 10, invalidID)); // Invalid parent |
| 456 | SC_TEST_EXPECT(not pool.createChildView(parentID, 90, 20, invalidID)); // Out of bounds |
| 457 | |
| 458 | // Verify resizing child view |
| 459 | pool.setNewBufferSize(childID, 5); // Resize to 5 ("World") |
| 460 | SC_TEST_EXPECT(pool.getReadableData(childID, childData)); |
| 461 | SC_TEST_EXPECT(childData.sizeInBytes() == 5); |
| 462 | SC_TEST_EXPECT(memcmp(childData.data(), "World", 5) == 0); |
| 463 | |
| 464 | pool.setNewBufferSize(childID, 10); // Try to expand back (should be ignored) |
| 465 | SC_TEST_EXPECT(pool.getReadableData(childID, childData)); |
no test coverage detected