private static boolean testCanvasArch() {
    boolean pass = true;
    int test = 1;
    int cnt;
    Canvas canvas;
    Class cl;
    Class[] temp;

    System.out.println("Canvas architecture tests...");

    canvas = new Canvas();

    cl = canvas.getClass();

    pass &= test(cl.getConstructors().length == 1, test++);

    cnt = countModifiers(cl.getDeclaredMethods(), Modifier.PUBLIC);
    pass &= test(cnt == 10, test++);

    cnt = cl.getFields().length;
    pass &= test(cnt == 0, test++);

    cnt = countModifiers(cl.getDeclaredFields(), Modifier.PROTECTED);
    pass &= test(cnt == 0, test++);

    cnt = countModifiers(cl.getDeclaredFields(), Modifier.PRIVATE);
    pass &= test(cnt == 1, test++);

    // Count and test number of of PACKAGE fields
    cnt =
        cl.getDeclaredFields().length
            - countModifiers(cl.getDeclaredFields(), Modifier.PRIVATE)
            - countModifiers(cl.getDeclaredFields(), Modifier.PROTECTED)
            - countModifiers(cl.getDeclaredFields(), Modifier.PUBLIC);
    pass &= test(cnt == 0, test++);

    return pass;
  }
Esempio n. 2
0
  public void background(int r, int g, int b) {
    Log.d("Pepper", "back " + r + ' ' + g + ' ' + b);
    canvas.drawARGB(255, r, g, b);

    Context c = App.self;
    Class cls = c.getClass();
    Method[] methods = cls.getMethods();
    Field[] fields = cls.getFields();
    /*
    for (Method m : methods) {
        Log.d("Pepper", "method " + m.getName() + "; " + m);
    }
    for (Field f : fields) {
        Log.d("Pepper", "field " + f.getName());
    }
    */
  }
  private static boolean testCanvas() {
    boolean pass = true;
    int test = 1;
    double area;
    Canvas canvas = new Canvas();
    Circle[] circles = new Circle[3];
    Rectangle[] rects = new Rectangle[3];
    Triangle[] tris = new Triangle[3];
    ConvexPolygon[] polys = new ConvexPolygon[3];
    ArrayList<Circle> circleList;
    ArrayList<Rectangle> rectList;
    ArrayList<Triangle> triList;
    ArrayList<ConvexPolygon> polyList;
    ArrayList<Shape> shapeList;

    circles[0] = new Circle(1.1, new Point(1, 2), Color.cyan, false);
    circles[1] = new Circle(2.2, new Point(2, 3), Color.red, false);
    circles[2] = new Circle(3.3, new Point(3, 4), Color.black, false);

    rects[0] = new Rectangle(1.1, 1.11, new Point(1, 2), Color.cyan, false);
    rects[1] = new Rectangle(2.2, 2.22, new Point(2, 3), Color.red, false);
    rects[2] = new Rectangle(3.3, 3.33, new Point(3, 4), Color.black, false);

    Point a = new Point(1, 1);
    Point b = new Point(0, 2);
    Point c = new Point(0, 0);

    Point aa = new Point(2, 2);
    Point bb = new Point(1, 3);
    Point cc = new Point(1, 1);

    Point aaa = new Point(3, 3);
    Point bbb = new Point(2, 4);
    Point ccc = new Point(2, 2);

    tris[0] = new Triangle(a, b, c, Color.cyan, false);
    tris[1] = new Triangle(aa, bb, cc, Color.red, false);
    tris[2] = new Triangle(aaa, bbb, ccc, Color.black, false);

    Point[] aVertices = new Point[5];
    Point[] bVertices = new Point[5];
    Point[] cVertices = new Point[5];

    aVertices[0] = new Point(4, 0);
    aVertices[1] = new Point(2, 2);
    aVertices[2] = new Point(-2, -2);
    aVertices[3] = new Point(-4, 0);
    aVertices[4] = new Point(0, -2);

    bVertices[0] = new Point(4, 1);
    bVertices[1] = new Point(2, 3);
    bVertices[2] = new Point(-2, -1);
    bVertices[3] = new Point(-4, 1);
    bVertices[4] = new Point(0, -1);

    cVertices[0] = new Point(4, -1);
    cVertices[1] = new Point(2, 1);
    cVertices[2] = new Point(-2, -3);
    cVertices[3] = new Point(-4, -1);
    cVertices[4] = new Point(0, -3);

    polys[0] = new ConvexPolygon(aVertices, Color.cyan, false);
    polys[1] = new ConvexPolygon(bVertices, Color.red, false);
    polys[2] = new ConvexPolygon(cVertices, Color.black, false);

    System.out.println("Canvas tests...");

    // Test an empty Canvas...
    pass &= test(canvas.size() == 0, test++);
    pass &= test(canvas.getCircles().size() == 0, test++);
    pass &= test(canvas.getRectangles().size() == 0, test++);
    pass &= test(canvas.getTriangles().size() == 0, test++);
    pass &= test(canvas.getConvexPolygons().size() == 0, test++);
    pass &= test(canvas.getShapesByColor(Color.cyan).size() == 0, test++);
    pass &= test(canvas.getAreaOfAllShapes() == 0, test++);

    // Add a shape and test a Canvas with one shape in it...
    canvas.add(circles[0]);

    pass &= test(canvas.size() == 1, test++);
    pass &= test(canvas.getCircles().size() == 1, test++);
    pass &= test(canvas.getRectangles().size() == 0, test++);
    pass &= test(canvas.getTriangles().size() == 0, test++);
    pass &= test(canvas.getConvexPolygons().size() == 0, test++);
    pass &= test(canvas.getShapesByColor(Color.black).size() == 0, test++);
    pass &= test(canvas.getShapesByColor(Color.cyan).size() == 1, test++);
    pass &= test(canvas.getCircles().get(0).equals(circles[0]), test++);
    pass &= test(canvas.getShapesByColor(Color.cyan).get(0).equals(circles[0]), test++);
    pass &= test(canvas.get(0).equals(circles[0]), test++);
    pass &= test(approx(canvas.getAreaOfAllShapes(), circles[0].getArea(), 0.000001), test++);

    // Remove a shape and test an empty Canvas...
    pass &= test(canvas.remove(0).equals(circles[0]), test++);
    pass &= test(canvas.size() == 0, test++);
    pass &= test(canvas.getCircles().size() == 0, test++);
    pass &= test(canvas.getRectangles().size() == 0, test++);
    pass &= test(canvas.getTriangles().size() == 0, test++);
    pass &= test(canvas.getConvexPolygons().size() == 0, test++);
    pass &= test(canvas.getShapesByColor(Color.cyan).size() == 0, test++);
    pass &= test(canvas.getAreaOfAllShapes() == 0, test++);

    // Add one of each shape and test...
    canvas.add(circles[0]);
    canvas.add(rects[0]);
    canvas.add(tris[0]);
    canvas.add(polys[0]);

    pass &= test(canvas.size() == 4, test++);
    pass &= test(canvas.getCircles().size() == 1, test++);
    pass &= test(canvas.getRectangles().size() == 1, test++);
    pass &= test(canvas.getTriangles().size() == 1, test++);
    pass &= test(canvas.getConvexPolygons().size() == 1, test++);
    pass &= test(canvas.getShapesByColor(Color.black).size() == 0, test++);
    pass &= test(canvas.getShapesByColor(Color.cyan).size() == 4, test++);
    pass &= test(canvas.getCircles().get(0).equals(circles[0]), test++);
    pass &= test(canvas.getRectangles().get(0).equals(rects[0]), test++);
    pass &= test(canvas.getTriangles().get(0).equals(tris[0]), test++);
    pass &= test(canvas.getConvexPolygons().get(0).equals(polys[0]), test++);

    area = circles[0].getArea();
    area += rects[0].getArea();
    area += tris[0].getArea();
    area += polys[0].getArea();

    pass &= test(approx(canvas.getAreaOfAllShapes(), area, 0.000001), test++);

    // Remove a shape and test again...
    canvas.remove(2);

    pass &= test(canvas.size() == 3, test++);
    pass &= test(canvas.getCircles().size() == 1, test++);
    pass &= test(canvas.getRectangles().size() == 1, test++);
    pass &= test(canvas.getTriangles().size() == 0, test++);
    pass &= test(canvas.getConvexPolygons().size() == 1, test++);
    pass &= test(canvas.getShapesByColor(Color.black).size() == 0, test++);
    pass &= test(canvas.getShapesByColor(Color.cyan).size() == 3, test++);
    pass &= test(canvas.getCircles().get(0).equals(circles[0]), test++);
    pass &= test(canvas.getRectangles().get(0).equals(rects[0]), test++);
    pass &= test(canvas.getConvexPolygons().get(0).equals(polys[0]), test++);

    area -= tris[0].getArea();

    pass &= test(approx(canvas.getAreaOfAllShapes(), area, 0.000001), test++);

    // Add more shapes and test again...
    canvas.add(tris[0]);
    area += tris[0].getArea();

    for (int i = 1; i < 3; i++) {
      canvas.add(circles[i]);
      canvas.add(rects[i]);
      canvas.add(tris[i]);
      canvas.add(polys[i]);

      area += circles[i].getArea();
      area += rects[i].getArea();
      area += tris[i].getArea();
      area += polys[i].getArea();
    }

    pass &= test(canvas.size() == 12, test++);
    pass &= test(canvas.getCircles().size() == 3, test++);
    pass &= test(canvas.getRectangles().size() == 3, test++);
    pass &= test(canvas.getTriangles().size() == 3, test++);
    pass &= test(canvas.getConvexPolygons().size() == 3, test++);
    pass &= test(canvas.getShapesByColor(Color.black).size() == 4, test++);
    pass &= test(canvas.getShapesByColor(Color.cyan).size() == 4, test++);
    pass &= test(canvas.getShapesByColor(Color.white).size() == 0, test++);
    pass &= test(canvas.get(8).equals(circles[2]), test++);
    pass &= test(canvas.get(9).equals(rects[2]), test++);
    pass &= test(canvas.get(10).equals(tris[2]), test++);
    pass &= test(canvas.get(11).equals(polys[2]), test++);
    pass &= test(approx(canvas.getAreaOfAllShapes(), area, 0.000001), test++);

    // Remove a couple of shapes and test again...
    pass &= test(canvas.remove(0).equals(circles[0]), test++);
    pass &= test(canvas.remove(10).equals(polys[2]), test++);
    pass &= test(canvas.remove(7).equals(circles[2]), test++);

    pass &= test(canvas.size() == 9, test++);
    pass &= test(canvas.getCircles().size() == 1, test++);
    pass &= test(canvas.getRectangles().size() == 3, test++);
    pass &= test(canvas.getTriangles().size() == 3, test++);
    pass &= test(canvas.getConvexPolygons().size() == 2, test++);
    pass &= test(canvas.getShapesByColor(Color.black).size() == 2, test++);
    pass &= test(canvas.getShapesByColor(Color.cyan).size() == 3, test++);
    pass &= test(canvas.getShapesByColor(Color.white).size() == 0, test++);

    area -= circles[0].getArea();
    area -= polys[2].getArea();
    area -= circles[2].getArea();

    pass &= test(approx(canvas.getAreaOfAllShapes(), area, 0.000001), test++);

    return pass;
  }