TestSqlHeap.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2023, Jelle Raaijmakers <jelle@gmta.nl>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ScopeGuard.h>
  7. #include <AK/StringBuilder.h>
  8. #include <LibCore/System.h>
  9. #include <LibSQL/Heap.h>
  10. #include <LibTest/TestCase.h>
  11. static constexpr auto db_path = "/tmp/test.db"sv;
  12. static NonnullRefPtr<SQL::Heap> create_heap()
  13. {
  14. auto heap = MUST(SQL::Heap::try_create(db_path));
  15. MUST(heap->open());
  16. return heap;
  17. }
  18. TEST_CASE(heap_write_large_storage_without_flush)
  19. {
  20. ScopeGuard guard([]() { MUST(Core::System::unlink(db_path)); });
  21. auto heap = create_heap();
  22. auto storage_block_id = heap->request_new_block_index();
  23. // Write large storage spanning multiple blocks
  24. StringBuilder builder;
  25. MUST(builder.try_append_repeated('x', SQL::Block::DATA_SIZE * 4));
  26. auto long_string = builder.string_view();
  27. TRY_OR_FAIL(heap->write_storage(storage_block_id, long_string.bytes()));
  28. // Read back
  29. auto stored_long_string = TRY_OR_FAIL(heap->read_storage(storage_block_id));
  30. EXPECT_EQ(long_string.bytes(), stored_long_string.bytes());
  31. }
  32. TEST_CASE(heap_write_large_storage_with_flush)
  33. {
  34. ScopeGuard guard([]() { MUST(Core::System::unlink(db_path)); });
  35. auto heap = create_heap();
  36. auto storage_block_id = heap->request_new_block_index();
  37. // Write large storage spanning multiple blocks
  38. StringBuilder builder;
  39. MUST(builder.try_append_repeated('x', SQL::Block::DATA_SIZE * 4));
  40. auto long_string = builder.string_view();
  41. TRY_OR_FAIL(heap->write_storage(storage_block_id, long_string.bytes()));
  42. MUST(heap->flush());
  43. // Read back
  44. auto stored_long_string = TRY_OR_FAIL(heap->read_storage(storage_block_id));
  45. EXPECT_EQ(long_string.bytes(), stored_long_string.bytes());
  46. }