File: operator_overload_runme.java

package info (click to toggle)
renderdoc 1.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 79,584 kB
  • sloc: cpp: 491,671; ansic: 285,823; python: 12,617; java: 11,345; cs: 7,181; makefile: 6,703; yacc: 5,682; ruby: 4,648; perl: 3,461; php: 2,119; sh: 2,068; lisp: 1,835; tcl: 1,068; ml: 747; xml: 137
file content (113 lines) | stat: -rw-r--r-- 2,686 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import operator_overload.*;

public class operator_overload_runme {

  static {
    System.loadLibrary("operator_overload");
  }

  public static void main(String argv[]) {

    // Java does not support operators, so we just check that these can be called as methods.

    Op.sanity_check();

    //test routine:
    Op a = new Op();
    Op b = new Op(5);
    Op c = new Op(b); // copy constructor
    Op d = new Op(2);
    Op dd = d.Equal(d); // assignment operator

    // test equality
    Assert(a.NotEqual(b));
    Assert(b.EqualEqual(c));
    Assert(a.NotEqual(d));
    Assert(d.EqualEqual(dd));

    // test <
    Assert(a.LessThan(b));
    Assert(a.LessThanEqual(b));
    Assert(b.LessThanEqual(c));
    Assert(b.GreaterThanEqual(c));
    Assert(b.GreaterThan(d));
    Assert(b.GreaterThanEqual(d));

    // test +=
    Op e = new Op(3);
    e.PlusEqual(d);
    Assert(e.EqualEqual(b));
    e.MinusEqual(c);
    Assert(e.EqualEqual(a));
    e = new Op(1);
    e.MultiplyEqual(b);
    Assert(e.EqualEqual(c));
    e.DivideEqual(d);
    Assert(e.EqualEqual(d));
    e.PercentEqual(c);
    Assert(e.EqualEqual(d));

    // test +
    Op f = new Op(1);
    Op g = new Op(1);
    Assert(f.Plus(g).EqualEqual(new Op(2)));
    Assert(f.Minus(g).EqualEqual(new Op(0)));
    Assert(f.Multiply(g).EqualEqual(new Op(1)));
    Assert(f.Divide(g).EqualEqual(new Op(1)));
    Assert(f.Percent(g).EqualEqual(new Op(0)));

    // test unary operators
    Assert((a.Not() == true));
    Assert((b.Not() == false));
    Assert(a.Minus().EqualEqual(a));
    Assert(b.Minus().EqualEqual( new Op(-5)));

    // test []
    Op h = new Op(3);
    Assert(h.__getitem__(0) == 3);
    Assert(h.__getitem__(1) == 0);
    h.__setitem__(0,2);	// set
    Assert(h.__getitem__(0) == 2);
    h.__setitem__(1,2);	// ignored
    Assert(h.IndexIntoConst(0) == 2);
    Assert(h.IndexIntoConst(1) == 0);

    // test ()
    Op i = new Op(3);
    Assert(i.Functor()==3);
    Assert(i.Functor(1)==4);
    Assert(i.Functor(1,2)==6);

    // test ++ --
    Op j = new Op(10);
    j.PlusPlusPrefix();
    j.PlusPlusPostfix(0);
    Assert(j.getI() == 12);
    j.MinusMinusPrefix();
    j.MinusMinusPostfix(0);
    Assert(j.getI() == 10);
    {
      Op op = j.PlusPlusPostfix(0);
      Assert(j.getI() == op.getI()+1);
    }
    {
      Op op = j.MinusMinusPostfix(0);
      Assert(j.getI() == op.getI()-1);
    }

    // cast operators
    Op k = new Op(3);
    int check_k = k.IntCast();
    Assert(check_k == 3);

    Op l = new Op(4);
    double check_l = l.DoubleCast();
    Assert(check_l == 4);

  }

  public static void Assert(boolean b) {
    if (!b)
      throw new RuntimeException("Assertion failed");
  }
}