test_core_runtime.odin 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package test_core_runtime
  2. import "core:fmt"
  3. import "base:intrinsics"
  4. import "core:mem"
  5. import "core:os"
  6. import "core:reflect"
  7. import "base:runtime"
  8. import "core:testing"
  9. TEST_count := 0
  10. TEST_fail := 0
  11. when ODIN_TEST {
  12. expect_value :: testing.expect_value
  13. } else {
  14. expect_value :: proc(t: ^testing.T, value, expected: $T, loc := #caller_location) -> bool where intrinsics.type_is_comparable(T) {
  15. TEST_count += 1
  16. ok := value == expected || reflect.is_nil(value) && reflect.is_nil(expected)
  17. if !ok {
  18. TEST_fail += 1
  19. fmt.printf("[%v] expected %v, got %v\n", loc, expected, value)
  20. }
  21. return ok
  22. }
  23. }
  24. main :: proc() {
  25. t := testing.T{}
  26. test_temp_allocator_big_alloc_and_alignment(&t)
  27. test_temp_allocator_alignment_boundary(&t)
  28. test_temp_allocator_returns_correct_size(&t)
  29. fmt.printf("%v/%v tests successful.\n", TEST_count - TEST_fail, TEST_count)
  30. if TEST_fail > 0 {
  31. os.exit(1)
  32. }
  33. }
  34. // Tests that having space for the allocation, but not for the allocation and alignment
  35. // is handled correctly.
  36. @(test)
  37. test_temp_allocator_alignment_boundary :: proc(t: ^testing.T) {
  38. arena: runtime.Arena
  39. context.allocator = runtime.arena_allocator(&arena)
  40. _, _ = mem.alloc(int(runtime.DEFAULT_ARENA_GROWING_MINIMUM_BLOCK_SIZE)-120)
  41. _, err := mem.alloc(112, 32)
  42. expect_value(t, err, nil)
  43. }
  44. // Tests that big allocations with big alignments are handled correctly.
  45. @(test)
  46. test_temp_allocator_big_alloc_and_alignment :: proc(t: ^testing.T) {
  47. arena: runtime.Arena
  48. context.allocator = runtime.arena_allocator(&arena)
  49. mappy: map[[8]int]int
  50. err := reserve(&mappy, 50000)
  51. expect_value(t, err, nil)
  52. }
  53. @(test)
  54. test_temp_allocator_returns_correct_size :: proc(t: ^testing.T) {
  55. arena: runtime.Arena
  56. context.allocator = runtime.arena_allocator(&arena)
  57. bytes, err := mem.alloc_bytes(10, 16)
  58. expect_value(t, err, nil)
  59. expect_value(t, len(bytes), 10)
  60. }