testing.odin 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package testing
  2. /*
  3. (c) Copyright 2024 Feoramund <[email protected]>.
  4. Made available under Odin's BSD-3 license.
  5. List of contributors:
  6. Ginger Bill: Initial implementation.
  7. Feoramund: Total rewrite.
  8. */
  9. import "base:intrinsics"
  10. import "base:runtime"
  11. import "core:log"
  12. import "core:reflect"
  13. import "core:sync"
  14. import "core:sync/chan"
  15. import "core:time"
  16. import "core:mem"
  17. _ :: reflect // alias reflect to nothing to force visibility for -vet
  18. _ :: mem // in case TRACKING_MEMORY is not enabled
  19. // IMPORTANT NOTE: Compiler requires this layout
  20. Test_Signature :: proc(^T)
  21. // IMPORTANT NOTE: Compiler requires this layout
  22. Internal_Test :: struct {
  23. pkg: string,
  24. name: string,
  25. p: Test_Signature,
  26. }
  27. Internal_Cleanup :: struct {
  28. procedure: proc(rawptr),
  29. user_data: rawptr,
  30. ctx: runtime.Context,
  31. }
  32. T :: struct {
  33. error_count: int,
  34. // If your test needs to perform random operations, it's advised to use
  35. // this value to seed a local random number generator rather than relying
  36. // on the non-thread-safe global one.
  37. //
  38. // This way, your results will be deterministic.
  39. //
  40. // This value is chosen at startup of the test runner, logged, and may be
  41. // specified by the user. It is the same for all tests of a single run.
  42. seed: u64,
  43. channel: Update_Channel_Sender,
  44. cleanups: [dynamic]Internal_Cleanup,
  45. // This allocator is shared between the test runner and its threads for
  46. // cloning log strings, so they can outlive the lifetime of individual
  47. // tests during channel transmission.
  48. _log_allocator: runtime.Allocator,
  49. _fail_now_called: bool,
  50. }
  51. fail :: proc(t: ^T, loc := #caller_location) {
  52. log.error("FAIL", location=loc)
  53. }
  54. // fail_now will cause a test to immediately fail and abort, much in the same
  55. // way a failed assertion or panic call will stop a thread.
  56. //
  57. // It is for when you absolutely need a test to fail without calling any of its
  58. // deferred statements. It will be cleaner than a regular assert or panic,
  59. // as the test runner will know to expect the signal this procedure will raise.
  60. fail_now :: proc(t: ^T, msg := "", loc := #caller_location) -> ! {
  61. t._fail_now_called = true
  62. if msg != "" {
  63. log.error("FAIL:", msg, location=loc)
  64. } else {
  65. log.error("FAIL", location=loc)
  66. }
  67. runtime.trap()
  68. }
  69. failed :: proc(t: ^T) -> bool {
  70. return t.error_count != 0
  71. }
  72. // cleanup registers a procedure and user_data, which will be called when the test, and all its subtests, complete.
  73. // Cleanup procedures will be called in LIFO (last added, first called) order.
  74. //
  75. // Each procedure will use a copy of the context at the time of registering,
  76. // and if the test failed due to a timeout, failed assertion, panic, bounds-checking error,
  77. // memory access violation, or any other signal-based fault, this procedure will
  78. // run with greater privilege in the test runner's main thread.
  79. //
  80. // That means that any cleanup procedure absolutely must not fail in the same way,
  81. // or it will take down the entire test runner with it. This is for when you
  82. // need something to run no matter what, if a test failed.
  83. //
  84. // For almost every usual case, `defer` should be preferable and sufficient.
  85. cleanup :: proc(t: ^T, procedure: proc(rawptr), user_data: rawptr) {
  86. append(&t.cleanups, Internal_Cleanup{procedure, user_data, context})
  87. }
  88. expect :: proc(t: ^T, ok: bool, msg: string = "", loc := #caller_location) -> bool {
  89. if !ok {
  90. log.error(msg, location=loc)
  91. }
  92. return ok
  93. }
  94. expectf :: proc(t: ^T, ok: bool, format: string, args: ..any, loc := #caller_location) -> bool {
  95. if !ok {
  96. log.errorf(format, ..args, location=loc)
  97. }
  98. return ok
  99. }
  100. expect_value :: proc(t: ^T, value, expected: $T, loc := #caller_location) -> bool where intrinsics.type_is_comparable(T) {
  101. ok := value == expected || reflect.is_nil(value) && reflect.is_nil(expected)
  102. if !ok {
  103. log.errorf("expected %v, got %v", expected, value, location=loc)
  104. }
  105. return ok
  106. }
  107. Memory_Verifier_Proc :: #type proc(t: ^T, ta: ^mem.Tracking_Allocator)
  108. expect_leaks :: proc(t: ^T, client_test: proc(t: ^T), verifier: Memory_Verifier_Proc) {
  109. when TRACKING_MEMORY {
  110. client_test(t)
  111. ta := (^mem.Tracking_Allocator)(context.allocator.data)
  112. sync.mutex_lock(&ta.mutex)
  113. // The verifier can inspect this local tracking allocator.
  114. // And then call `testing.expect_*` as makes sense for the client test.
  115. verifier(t, ta)
  116. sync.mutex_unlock(&ta.mutex)
  117. clear(&ta.bad_free_array)
  118. free_all(context.allocator)
  119. }
  120. }
  121. set_fail_timeout :: proc(t: ^T, duration: time.Duration, loc := #caller_location) {
  122. chan.send(t.channel, Event_Set_Fail_Timeout {
  123. at_time = time.time_add(time.now(), duration),
  124. location = loc,
  125. })
  126. }