-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInternalTestFunctions.java
144 lines (122 loc) · 3.91 KB
/
InternalTestFunctions.java
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package be.ugent.idlab.knows.functions.internalfunctions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
/**
* <p>Copyright 2022 IDLab (Ghent University - imec)</p>
*
* This class contains functions that are "in the class path", for testing purposes
*
* @author Gerald Haesendonck
*/
public class InternalTestFunctions {
private static boolean closed = false;
private static final Logger logger = LoggerFactory.getLogger(InternalTestFunctions.class);
/**
* Returns the sum of a and b
* @param a your lucky number
* @param b my lucky number
* @return our lucky number (a + b)
*/
public static long sum(long a, long b) {
return a + b;
}
/**
* Returns the length of a raw list, i.e. without type parameter. This is to check if data conversion works without
* knowing the type of the elements of the list.
* @param aRawList The raw list to return the length of
* @return The length of the raw list
*/
public static long rawListLen(final List aRawList) {
return aRawList.size();
}
/**
* Returns the sum of a and b
* @param a your lucky number
* @param b my lucky number
* @return our lucky number (a * b)
*/
public static long multiply(long a, long b) {
return a * b;
}
/**
* Returns a^b, with b >= 0
* @param a base
* @param b exponent, greater than or equal to 0
* @return a to the power of b
*/
public static long pow(long a, long b) {
// uses exponentation by squaring
if(b<0){
throw new IllegalStateException("b must be larger than 0");
}
if (b == 0) {
return 1;
}
if (b % 2 == 0) {
return pow(a * a, b / 2);
}
return a * pow(a * a, (b - 1) / 2);
}
public static Long writeToFile(Object o, String filename){
try (FileWriter fileWriter = new FileWriter(filename)){
fileWriter.write(o.toString());
}
catch (IOException ioException){
logger.error("error occurred while writing to file: {}", ioException.toString());
return 1L;
}
return 0L;
}
public static void writeToFileNoReturn(Object o, String filename){
try (FileWriter fileWriter = new FileWriter(filename)){
fileWriter.write(o.toString());
}
catch (IOException ioException){
logger.error("error occurred while writing to file: {}", ioException.toString());
}
}
public static Object tee(Object object, String filename){
writeToFile(object, filename);
return object;
}
public static List<?> makeSeq(List<?> objects){
return objects;
}
public static List<?> makeListFromSeq(List<?> objects){
return objects;
}
public static Long testExceptionFunction(Long a) throws Exception{
if(a <= 0){
throw new Exception("a should be greater than 0");
}
return a;
}
public static int testVarargsFunction(Object... objects){
return objects.length;
}
public static void testVoidReturnFunction(Long l){
System.out.print("");
}
public static Long testNoParameters(){
return 1L;
}
public static void testMultipleExceptions() throws IOException, NullPointerException{
throw new IOException("ioException");
}
public static String concatSequence(final List<CharSequence> seq, final CharSequence delimiter) {
final CharSequence sep = delimiter == null? "" : delimiter;
return String.join(sep, seq);
}
public static void close() {
closed = true;
}
public static void close(final int number) {
logger.debug("This is number {}", number);
}
public static boolean isClosed() {
return closed;
}
}