tv_clear_spec.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. local helpers = require('test.unit.helpers')(after_each)
  2. local itp = helpers.gen_itp(it)
  3. local eval_helpers = require('test.unit.eval.helpers')
  4. local alloc_log_new = helpers.alloc_log_new
  5. local cimport = helpers.cimport
  6. local ffi = helpers.ffi
  7. local eq = helpers.eq
  8. local a = eval_helpers.alloc_logging_helpers
  9. local type_key = eval_helpers.type_key
  10. local list_type = eval_helpers.list_type
  11. local list_items = eval_helpers.list_items
  12. local dict_items = eval_helpers.dict_items
  13. local lua2typvalt = eval_helpers.lua2typvalt
  14. local lib = cimport('./src/nvim/eval/typval.h', './src/nvim/eval.h')
  15. local alloc_log = alloc_log_new()
  16. before_each(function()
  17. alloc_log:before_each()
  18. end)
  19. after_each(function()
  20. alloc_log:after_each()
  21. end)
  22. describe('tv_clear()', function()
  23. itp('successfully frees all lists in [&l [1], *l, *l]', function()
  24. local l_inner = {1}
  25. local list = {l_inner, l_inner, l_inner}
  26. local list_tv = ffi.gc(lua2typvalt(list), nil)
  27. local list_p = list_tv.vval.v_list
  28. local lis = list_items(list_p)
  29. local list_inner_p = lis[1].li_tv.vval.v_list
  30. local lis_inner = list_items(list_inner_p)
  31. alloc_log:check({
  32. a.list(list_p),
  33. a.list(list_inner_p),
  34. a.li(lis_inner[1]),
  35. a.li(lis[1]),
  36. a.li(lis[2]),
  37. a.li(lis[3]),
  38. })
  39. eq(3, list_inner_p.lv_refcount)
  40. lib.tv_clear(list_tv)
  41. alloc_log:check({
  42. a.freed(lis_inner[1]),
  43. a.freed(list_inner_p),
  44. a.freed(lis[1]),
  45. a.freed(lis[2]),
  46. a.freed(lis[3]),
  47. a.freed(list_p),
  48. })
  49. end)
  50. itp('successfully frees all lists in [&l [], *l, *l]', function()
  51. local l_inner = {[type_key]=list_type}
  52. local list = {l_inner, l_inner, l_inner}
  53. local list_tv = ffi.gc(lua2typvalt(list), nil)
  54. local list_p = list_tv.vval.v_list
  55. local lis = list_items(list_p)
  56. local list_inner_p = lis[1].li_tv.vval.v_list
  57. alloc_log:check({
  58. a.list(list_p),
  59. a.list(list_inner_p),
  60. a.li(lis[1]),
  61. a.li(lis[2]),
  62. a.li(lis[3]),
  63. })
  64. eq(3, list_inner_p.lv_refcount)
  65. lib.tv_clear(list_tv)
  66. alloc_log:check({
  67. a.freed(list_inner_p),
  68. a.freed(lis[1]),
  69. a.freed(lis[2]),
  70. a.freed(lis[3]),
  71. a.freed(list_p),
  72. })
  73. end)
  74. itp('successfully frees all dictionaries in [&d {}, *d]', function()
  75. local d_inner = {}
  76. local list = {d_inner, d_inner}
  77. local list_tv = ffi.gc(lua2typvalt(list), nil)
  78. local list_p = list_tv.vval.v_list
  79. local lis = list_items(list_p)
  80. local dict_inner_p = lis[1].li_tv.vval.v_dict
  81. alloc_log:check({
  82. a.list(list_p),
  83. a.dict(dict_inner_p),
  84. a.li(lis[1]),
  85. a.li(lis[2]),
  86. })
  87. eq(2, dict_inner_p.dv_refcount)
  88. lib.tv_clear(list_tv)
  89. alloc_log:check({
  90. a.freed(dict_inner_p),
  91. a.freed(lis[1]),
  92. a.freed(lis[2]),
  93. a.freed(list_p),
  94. })
  95. end)
  96. itp('successfully frees all dictionaries in [&d {a: 1}, *d]', function()
  97. local d_inner = {a=1}
  98. local list = {d_inner, d_inner}
  99. local list_tv = ffi.gc(lua2typvalt(list), nil)
  100. local list_p = list_tv.vval.v_list
  101. local lis = list_items(list_p)
  102. local dict_inner_p = lis[1].li_tv.vval.v_dict
  103. local dis = dict_items(dict_inner_p)
  104. alloc_log:check({
  105. a.list(list_p),
  106. a.dict(dict_inner_p),
  107. a.di(dis.a, 1),
  108. a.li(lis[1]),
  109. a.li(lis[2]),
  110. })
  111. eq(2, dict_inner_p.dv_refcount)
  112. lib.tv_clear(list_tv)
  113. alloc_log:check({
  114. a.freed(dis.a),
  115. a.freed(dict_inner_p),
  116. a.freed(lis[1]),
  117. a.freed(lis[2]),
  118. a.freed(list_p),
  119. })
  120. end)
  121. end)