TestWeakMap.hx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import haxe.ds.WeakMap;
  2. using Lambda;
  3. /**
  4. The goal with this test is to test the 'weak' behaviour of WeakMap.
  5. If it works, it will run forever, with constant memory usage;
  6. Otherwise it will exhaust the memory
  7. **/
  8. class TestWeakMap
  9. {
  10. static function main()
  11. {
  12. #if sys
  13. var iterate = Sys.args().has('-iterate');
  14. #else
  15. var iterate = true;
  16. #end
  17. var map = new WeakMap(),
  18. saved = new List();
  19. var i = 0;
  20. while(true)
  21. {
  22. i++;
  23. var obj = { count:i };
  24. map.set(obj, i);
  25. if (i % 77 == 0)
  26. {
  27. // test also some strong references
  28. if (i & 1 == 0)
  29. saved.add(obj);
  30. else
  31. saved.push(obj);
  32. if (saved.length > 1000)
  33. saved.pop();
  34. //check if all references are still present
  35. for (s in saved)
  36. {
  37. var val = map.get(s);
  38. if (val == null)
  39. trace('ERROR: Reference from $s not found!');
  40. if (val != s.count)
  41. trace('ERROR: Wrong value for $s: $val');
  42. }
  43. } else if (iterate && i % 10000 == 0) {
  44. var count = 0;
  45. for (v in map.keys())
  46. {
  47. count++;
  48. if (v == null) trace("ITERATION ERROR:NULL");
  49. var val = map.get(v);
  50. if (val != v.count)
  51. trace('ITERATION ERROR: $val != ${v.count}');
  52. }
  53. #if false
  54. Sys.print('$count (${count / saved.length}) \r');
  55. #else
  56. trace(count, count / saved.length);
  57. #end
  58. }
  59. }
  60. }
  61. }