Browse Source

[tests] added weakmap stress test

Cauê Waneck 11 năm trước cách đây
mục cha
commit
10c07bbc42
2 tập tin đã thay đổi với 62 bổ sung0 xóa
  1. 60 0
      tests/misc/weakmap/TestWeakMap.hx
  2. 2 0
      tests/misc/weakmap/compile-java.hxml

+ 60 - 0
tests/misc/weakmap/TestWeakMap.hx

@@ -0,0 +1,60 @@
+import haxe.ds.WeakMap;
+using Lambda;
+
+/**
+	The goal with this test is to test the 'weak' behaviour of WeakMap.
+	If it works, it will run forever, with constant memory usage;
+	Otherwise it will exhaust the memory
+**/
+class TestWeakMap
+{
+
+	static function main()
+	{
+		var map = new WeakMap(),
+				saved = new List();
+		var i = 0;
+		while(true)
+		{
+			i++;
+			var obj = { count:i };
+			map.set(obj, i);
+			if (i % 77 == 0)
+			{
+				// test also some strong references
+				if (i & 1 == 0)
+					saved.add(obj);
+				else
+					saved.push(obj);
+				if (saved.length > 1000)
+					saved.pop();
+
+				//check if all references are still present
+				for (s in saved)
+				{
+					var val = map.get(s);
+					if (val == null)
+						trace('ERROR: Reference from $s not found!');
+					if (val != s.count)
+						trace('ERROR: Wrong value for $s: $val');
+				}
+			} else if (i % 10000 == 0) {
+				var count = 0;
+				for (v in map.keys())
+				{
+					count++;
+					if (v == null) trace("ITERATION ERROR:NULL");
+					var val = map.get(v);
+					if (val != v.count)
+						trace('ITERATION ERROR: $val != ${v.count}');
+				}
+#if sys
+				Sys.print('$count (${count / saved.length}) \r');
+#else
+				trace(count, count / saved.length);
+#end
+			}
+		}
+	}
+
+}

+ 2 - 0
tests/misc/weakmap/compile-java.hxml

@@ -0,0 +1,2 @@
+-main TestWeakMap
+-java java