TestMalloc.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. * Copyright (c) 2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h>
  7. #include <LibC/mallocdefs.h>
  8. #include <errno.h>
  9. #include <stdlib.h>
  10. TEST_CASE(malloc_limits)
  11. {
  12. EXPECT_NO_CRASH("Allocation of 0 size should succed at allocation and release", [] {
  13. errno = 0;
  14. void* ptr = malloc(0);
  15. EXPECT_EQ(errno, 0);
  16. free(ptr);
  17. return Test::Crash::Failure::DidNotCrash;
  18. });
  19. EXPECT_NO_CRASH("Allocation of the maximum `size_t` value should fails with `ENOMEM`", [] {
  20. errno = 0;
  21. void* ptr = malloc(NumericLimits<size_t>::max());
  22. EXPECT_EQ(errno, ENOMEM);
  23. EXPECT_EQ(ptr, nullptr);
  24. free(ptr);
  25. return Test::Crash::Failure::DidNotCrash;
  26. });
  27. EXPECT_NO_CRASH("Allocation of the maximum `size_t` value that does not overflow should fails with `ENOMEM`", [] {
  28. errno = 0;
  29. void* ptr = malloc(NumericLimits<size_t>::max() - ChunkedBlock::block_size - sizeof(BigAllocationBlock));
  30. EXPECT_EQ(errno, ENOMEM);
  31. EXPECT_EQ(ptr, nullptr);
  32. free(ptr);
  33. return Test::Crash::Failure::DidNotCrash;
  34. });
  35. }