test_core_thread.odin 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package test_core_thread
  2. import "core:testing"
  3. import "core:thread"
  4. import "core:fmt"
  5. import "core:os"
  6. TEST_count := 0
  7. TEST_fail := 0
  8. t := &testing.T{}
  9. when ODIN_TEST {
  10. expect :: testing.expect
  11. log :: testing.log
  12. } else {
  13. expect :: proc(t: ^testing.T, condition: bool, message: string, loc := #caller_location) {
  14. TEST_count += 1
  15. if !condition {
  16. TEST_fail += 1
  17. fmt.printf("[%v] %v\n", loc, message)
  18. return
  19. }
  20. }
  21. log :: proc(t: ^testing.T, v: any, loc := #caller_location) {
  22. fmt.printf("[%v] ", loc)
  23. fmt.printf("log: %v\n", v)
  24. }
  25. }
  26. main :: proc() {
  27. poly_data_test(t)
  28. if TEST_fail > 0 {
  29. os.exit(1)
  30. }
  31. }
  32. @(test)
  33. poly_data_test :: proc(_t: ^testing.T) {
  34. MAX :: size_of(rawptr) * thread.MAX_USER_ARGUMENTS
  35. @static poly_data_test_t: ^testing.T
  36. poly_data_test_t = _t
  37. b: [MAX]byte = 8
  38. t1 := thread.create_and_start_with_poly_data(b, proc(b: [MAX]byte) {
  39. b_expect: [MAX]byte = 8
  40. expect(poly_data_test_t, b == b_expect, "thread poly data not correct")
  41. })
  42. defer free(t1)
  43. b1: [3]uintptr = 1
  44. b2: [MAX / 2]byte = 3
  45. t2 := thread.create_and_start_with_poly_data2(b1, b2, proc(b: [3]uintptr, b2: [MAX / 2]byte) {
  46. b_expect: [3]uintptr = 1
  47. b2_expect: [MAX / 2]byte = 3
  48. expect(poly_data_test_t, b == b_expect, "thread poly data not correct")
  49. expect(poly_data_test_t, b2 == b2_expect, "thread poly data not correct")
  50. })
  51. defer free(t2)
  52. t3 := thread.create_and_start_with_poly_data3(b1, b2, uintptr(333), proc(b: [3]uintptr, b2: [MAX / 2]byte, b3: uintptr) {
  53. b_expect: [3]uintptr = 1
  54. b2_expect: [MAX / 2]byte = 3
  55. expect(poly_data_test_t, b == b_expect, "thread poly data not correct")
  56. expect(poly_data_test_t, b2 == b2_expect, "thread poly data not correct")
  57. expect(poly_data_test_t, b3 == 333, "thread poly data not correct")
  58. })
  59. defer free(t3)
  60. t4 := thread.create_and_start_with_poly_data4(uintptr(111), b1, uintptr(333), u8(5), proc(n: uintptr, b: [3]uintptr, n2: uintptr, n4: u8) {
  61. b_expect: [3]uintptr = 1
  62. expect(poly_data_test_t, n == 111, "thread poly data not correct")
  63. expect(poly_data_test_t, b == b_expect, "thread poly data not correct")
  64. expect(poly_data_test_t, n2 == 333, "thread poly data not correct")
  65. expect(poly_data_test_t, n4 == 5, "thread poly data not correct")
  66. })
  67. defer free(t4)
  68. thread.join_multiple(t1, t2, t3, t4)
  69. }