blob: 0d4bf065ea4fc7bd0c7f2fe3fe850d8dd59e3079 [file] [log] [blame]
Calin Juravle3173c8a2015-02-23 15:53:39 +00001/*
2* Copyright (C) 2015 The Android Open Source Project
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17
18public class Main {
19
20 public static void main(String[] args) {
21 assertEqual(4, add3(1));
22 assertEqual(4l, add3(1l));
23 assertEqual(4f, add3(1f));
24 assertEqual(4d, add3(1d));
25 }
26
27 public static int add3(int a) {
28 return 1 + a + 2;
29 }
30
31 public static long add3(long a) {
32 return 1l + a + 2l;
33 }
34
35 public static float add3(float a) {
36 return 1f + a + 2f;
37 }
38
39 public static double add3(double a) {
40 return 1d + a + 2d;
41 }
42
43 public static void assertEqual(int a, int b) {
44 if (a != b) {
45 throw new RuntimeException("Expected: " + a + " Found: " + b);
46 }
47 }
48
49 public static void assertEqual(long a, long b) {
50 if (a != b) {
51 throw new RuntimeException("Expected: " + a + " Found: " + b);
52 }
53 }
54
55 public static void assertEqual(float a, float b) {
56 boolean aproxEquals = (a > b)
57 ? ((a - b) < 0.0001f)
58 : ((b - a) < 0.0001f);
59 if (!aproxEquals) {
60 throw new RuntimeException("Expected: " + a + " Found: " + b);
61 }
62 }
63
64 public static void assertEqual(double a, double b) {
65 boolean aproxEquals = (a > b)
66 ? ((a - b) < 0.0001d)
67 : ((b - a) < 0.0001d);
68 if (!aproxEquals) {
69 throw new RuntimeException("Expected: " + a + " Found: " + b);
70 }
71 }
72}