// This tests almost all operations on 2D points, and also some // operations on 2D RectDomains. It should probably not be in the array // test suite. class HelloWorld { public static int testCount; public static void printp1(Point<1> p) { System.out.print("["); System.out.print(p[1]); System.out.print("]"); } public static void printp2(Point<2> p) { System.out.print("["); System.out.print(p[1]); System.out.print(", "); System.out.print(p[2]); System.out.print("]"); } public static void printp3(Point<3> p) { System.out.print("["); System.out.print(p[1]); System.out.print(", "); System.out.print(p[2]); System.out.print(", "); System.out.print(p[3]); System.out.print("]"); } public static void printrd2(RectDomain<2> r) { System.out.print("["); printp2(r.lwb()); System.out.print(" : "); printp2(r.upb()); if (r.stride() != [1, 1]) { System.out.print(" : "); printp2(r.stride()); } System.out.print("]"); } public static void test2(String testName, Point<2> a, Point<2> b) { testCount++; if (a != b) { System.out.print("Failure on test "); System.out.print(testCount); System.out.print(" \""); System.out.print(testName); System.out.print("\": "); printp2(a); System.out.print(" != "); printp2(b); System.out.println(""); } } public static void testr2(String testName, RectDomain<2> a, RectDomain<2> b) { testCount++; if (a.isEmpty() && b.isEmpty()) return; if (a != b) { System.out.print("Failure on test "); System.out.print(testCount); System.out.print(" \""); System.out.print(testName); System.out.print("\": "); printrd2(a); System.out.print(" != "); printrd2(b); System.out.println(""); } } public static void testIntersection_UnitStride(RectDomain<2> r, RectDomain<2> s) { Point<2> min = [(r.min()[1] < s.min()[1]) ? s.min()[1] : r.min()[1], (r.min()[2] < s.min()[2]) ? s.min()[2] : r.min()[2]]; Point<2> max = [(r.max()[1] > s.max()[1]) ? s.max()[1] : r.max()[1], (r.max()[2] > s.max()[2]) ? s.max()[2] : r.max()[2]]; RectDomain<2> ans = [min : max]; if (!(r * s).isEmpty()) { test2("rd<2> intersection lower bound", min, (r * s).min()); test2("rd<2> intersection upper bound", max, (r * s).max()); if (min != (r * s).min() || max != (r * s).max()) { System.out.print("r = "); printrd2(r); System.out.print(" s = "); printrd2(s); System.out.print("; min = "); printp2(min); System.out.print(" max = "); printp2(max); System.out.print("; r * s = "); printrd2(r * s); System.out.println(""); } } testr2("intersection", ans, r * s); } public static void testSize(RectDomain<2> r) { int i = 0; foreach (p in r) i++; if (i != r.size()) { System.out.print("Size test failed: "); printrd2(r); System.out.print(".size() => "); System.out.print(r.size()); System.out.print(" (should be "); System.out.print(i); System.out.println(")"); } } public static void testContains(RectDomain<2> r, Point<2> q) { boolean contains = false; foreach (p in r) if (p == q) contains = true; if (contains != r.contains(q)) System.out.println("Contains test failed!"); } public static void testSubset(RectDomain<2> r, RectDomain<2> s) { if ((r <= s) != (r * s == r)) System.out.println("Subset test failed!"); if ((s <= r) != (s * r == s)) System.out.println("Subset test failed!"); } public static void main( String[] argv ) { Point<2> p = [2, 3]; RectDomain<2> r, empty = [[2, 1] : [0, 0]]; int [2d] a; int temp; // Test 2d points p = p + p; test2("+", p, [4, 6]); p = p - p; test2("-", p, [0, 0]); p = Point<2>.all(1); test2("all", p, [1, 1]); p = Point<2>.direction(1); test2("direction1", p, [1, 0]); p = Point<2>.direction(1, 1); test2("direction2", p, [1, 0]); p = Point<2>.direction(2, 2); test2("direction3", p, [0, 2]); p = Point<2>.direction(-2, 2); test2("direction4", p, [0, -2]); p = p.all(8); test2("all", p, [8, 8]); p = [-1, -2] / 2; test2("div", p, [-1, -1]); p = [1, 4] / -2; test2("div2", p, [-1, -2]); p = [2, 3] < [3, 4] ? [2, 3] : [3, 4]; test2("<", p, [2, 3]); p = [2, 3] > [3, 4] ? [2, 3] : [3, 4]; test2(">", p, [3, 4]); p = Point<2>.all(p.arity()); test2("arity", p, [2, 2]); test2("+", [1, 4] + [5, 7], [6, 11]); test2("-", [1, 4] - [5, 9], [-4, -5]); test2("*", [2, 11] * [6, 2], [12, 22]); test2("/", [2, -11] / [6, 2], [0, -6]); test2("unary-", -[2, -11], [-2, 11]); test2("unary-", -[0, -4], [0, 4]); /* p = [-1, -1]; p = p.set(1, 8); test2("set", p, [8, -1]); p = p.set(2, 5); test2("set2", p, [8, 5]); */ System.out.println("Finished testing 2d points."); r = [[-3, -4] : [3, 8]]; test2("lwb", r.lwb(), [-3, -4]); test2("upb", r.upb(), [4, 9]); test2("min", r.min(), [-3, -4]); test2("max", r.max(), [3, 8]); a = new int[r]; p = Point<2>.all(0); for (int i = -3; i < 4; i++) for (int j = -4; j < 9; j++) p = p + [i, j]; foreach (q in r) p = p - q; test2("foreach+/for-", p, [0, 0]); testSubset(r, r); testSubset(r, empty); testIntersection_UnitStride(r, r); testIntersection_UnitStride(r, empty); foreach (lo in [[-5, -5] : [5, 10] : [2, 2]]) foreach (size in [[0, 0] : [15, 15] : [2, 2]]) { testIntersection_UnitStride(r, [lo : lo + size]); testSize([lo : lo + size]); testSize(r * [lo : lo + size]); testContains(r, lo); testSubset(r, [lo : lo + size]); } System.out.println("Finished testing 2d RectDomains."); } }