View Javadoc
1   public class ArgumentAssertions {
2       // Should fail
3       public static int getAbsAdd(int x, int y) {
4           assert x != Integer.MIN_VALUE;
5           assert y != Integer.MIN_VALUE;
6           int absX = Math.abs(x);
7           int absY = Math.abs(y);
8           assert (absX <= Integer.MAX_VALUE - absY);
9           return absX + absY;
10      }
11
12      // Should fail
13      public static int getAbsAdd2(int x, int y) {
14          assert Math.abs(x) != Integer.MIN_VALUE;
15          assert y != Integer.MIN_VALUE;
16          int absX = Math.abs(x);
17          int absY = Math.abs(y);
18          assert (absX <= Integer.MAX_VALUE - absY);
19          return absX + absY;
20      }
21
22      private static int plus1(int x) {
23          return x + 1;
24      }
25
26      // Should fail
27      public static int getAbsAdd3(int x, int y) {
28          assert Math.abs(x) != Integer.MIN_VALUE;
29          assert plus1(y) != Integer.MIN_VALUE;
30          assert Math.abs(plus1(y)) != Integer.MIN_VALUE;
31          int absX = Math.abs(x);
32          int absY = Math.abs(y);
33          assert (absX <= Integer.MAX_VALUE - absY);
34          return absX + absY;
35      }
36
37      // Should fail
38      public static int getAbsAdd4(int x, int y) {
39          assert Math.abs(plus1(y)) != Integer.MIN_VALUE;
40          int absX = Math.abs(x);
41          int absY = Math.abs(y);
42          return absX + absY;
43      }
44
45      // Should fail
46      public static Object print(Object x) {
47          assert x != null;
48          Double f = 3.0;
49          System.out.println(f.toString());
50          return x;
51      }
52
53      private static String helper(String s) {
54          return s;
55      }
56
57      // Should fail
58      public static void indirect(String s) {
59          assert helper(s) == null : "This is not a false pos" + s;
60      }
61
62      // Should fail
63      public static boolean compBool1(boolean b) {
64          assert b == true;
65          return b;
66      }
67
68      // Should fail
69      public static boolean compBool2(boolean b) {
70          assert b;
71          return b;
72      }
73
74      // Should fail
75      public static int getAbs(byte x) {
76          assert Math.abs(x) != Integer.MIN_VALUE;
77          int absX = Math.abs(x);
78          return absX;
79      }
80
81      // Should fail
82      public static int getAbs(short x) {
83          assert Math.abs(x) != Integer.MIN_VALUE;
84          int absX = Math.abs(x);
85          return absX;
86      }
87
88      // Should fail
89      public static int compShor(short s) {
90          assert s == 0;
91          return s;
92      }
93
94      // Should fail
95      public static long getAbs(long x) {
96          assert Math.abs(x) != Integer.MIN_VALUE;
97          long absX = Math.abs(x);
98          return absX;
99      }
100
101     // Should fail
102     public static long compLong(long l) {
103         assert l == 0;
104         return l;
105     }
106
107     // Should fail
108     public static float getAbs(float x) {
109         assert Math.abs(x) != Integer.MIN_VALUE;
110         float absX = Math.abs(x);
111         return absX;
112     }
113
114     // Should fail
115     public static float compFloat(float f) {
116         assert f < 0;
117         return f;
118     }
119
120     // Should fail
121     public static double getAbs(double x) {
122         assert Math.abs(x) != Integer.MIN_VALUE;
123         double absX = Math.abs(x);
124         return absX;
125     }
126
127     // Should fail
128     public static double compDouble(double d) {
129         assert d > 0;
130         return d;
131     }
132
133     // Should fail
134     public static boolean indirect(boolean b) {
135         boolean l = b;
136         assert l;
137         return l;
138     }
139
140     // Should fail, but passes because indirect assertions of arguments are not supported yet (except copies)
141     public static boolean indirect2(boolean b) {
142         boolean l = !b;
143         assert l;
144         return l;
145     }
146
147     // Should pass
148     public static String literal(String s) {
149         assert false;
150         return s;
151     }
152
153     // Should pass
154     public static boolean literalAndMessage(boolean b) {
155         assert false : b;
156         return b;
157     }
158
159     // Should pass
160     public static String literalAndMessageStr(String s) {
161         assert false : s;
162         return s;
163     }
164
165     // Should pass
166     public static void conditionallyInMessage(int x, String s) {
167         if (x < 0) {
168             assert false : "This is a false pos" + s;
169         }
170     }
171
172     // helper method
173     public void helper() {
174         privateMethod(1);
175         privateFinalMethod(1);
176         privateStaticMethod(0);
177     }
178
179     // Should pass - private method
180     private void privateMethod(int x) {
181         assert x != 0;
182     }
183
184     // Should pass - private method
185     private final void privateFinalMethod(int x) {
186         assert x != 0;
187     }
188
189     // Should pass - private method
190     private static void privateStaticMethod(int x) {
191         assert x == 0;
192     }
193
194     // Should fail
195     public static void assertingArgInFor(String prefix) {
196         java.util.List<String> strings = new java.util.ArrayList<>();
197         for (String s : strings) {
198             assert s.startsWith(prefix);
199         }
200     }
201
202     // Should fail
203     public static void assertingArgInStream(String prefix) {
204         java.util.List<String> strings = new java.util.ArrayList<>();
205         strings.forEach(s -> {
206             assert s.startsWith(prefix);
207         });
208     }
209 }