Skip to content

Commit 4ecead4

Browse files
Merge remote-tracking branch 'origin/master'
2 parents caa8387 + 1a05305 commit 4ecead4

149 files changed

Lines changed: 3850 additions & 81 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎groovy-spock/pom.xml‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>org.spockframework</groupId>
6+
<artifactId>groovy-spock</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
<name>Spock Framework - Example Project</name>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
14+
</properties>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.codehaus.gmavenplus</groupId>
20+
<artifactId>gmavenplus-plugin</artifactId>
21+
<version>1.5</version>
22+
<executions>
23+
<execution>
24+
<goals>
25+
<goal>compile</goal>
26+
<goal>testCompile</goal>
27+
</goals>
28+
</execution>
29+
</executions>
30+
</plugin>
31+
</plugins>
32+
</build>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.spockframework</groupId>
37+
<artifactId>spock-core</artifactId>
38+
<version>1.0-groovy-2.4</version>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.codehaus.groovy</groupId>
43+
<artifactId>groovy-all</artifactId>
44+
<version>2.4.7</version>
45+
</dependency>
46+
</dependencies>
47+
</project>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import spock.lang.Specification
2+
3+
class FirstSpecification extends Specification {
4+
5+
def "one plus one should equal two"() {
6+
expect:
7+
1 + 1 == 2
8+
}
9+
10+
def "two plus two should equal four"() {
11+
given:
12+
int left = 2
13+
int right = 2
14+
15+
when:
16+
int result = left + right
17+
18+
then:
19+
result == 4
20+
}
21+
22+
def "Should be able to remove from list"() {
23+
given:
24+
def list = [1, 2, 3, 4]
25+
26+
when:
27+
list.remove(0)
28+
29+
then:
30+
list == [2, 3, 4]
31+
}
32+
33+
def "Should get an index out of bounds when removing a non-existent item"() {
34+
given:
35+
def list = [1, 2, 3, 4]
36+
37+
when:
38+
list.remove(20)
39+
40+
then:
41+
thrown(IndexOutOfBoundsException)
42+
list.size() == 4
43+
}
44+
45+
def "numbers to the power of two"(int a, int b, int c) {
46+
expect:
47+
Math.pow(a, b) == c
48+
49+
where:
50+
a | b | c
51+
1 | 2 | 1
52+
2 | 2 | 4
53+
3 | 2 | 9
54+
}
55+
56+
def "Should return default value for mock"() {
57+
given:
58+
def paymentGateway = Mock(PaymentGateway)
59+
60+
when:
61+
def result = paymentGateway.makePayment(12.99)
62+
63+
then:
64+
!result
65+
}
66+
67+
def "Should return true value for mock"() {
68+
given:
69+
def paymentGateway = Mock(PaymentGateway)
70+
paymentGateway.makePayment(20) >> true
71+
72+
when:
73+
def result = paymentGateway.makePayment(20)
74+
75+
then:
76+
result
77+
}
78+
79+
def "Should verify notify was called"() {
80+
given:
81+
def notifier = Mock(Notifier)
82+
83+
when:
84+
notifier.notify('foo')
85+
86+
then:
87+
1 * notifier.notify('foo')
88+
}
89+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface Notifier {
2+
void notify(String message)
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface PaymentGateway {
2+
boolean makePayment(BigDecimal amount)
3+
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
package org.baeldung.guava;
2+
3+
import com.google.common.math.DoubleMath;
4+
import com.google.common.math.IntMath;
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.junit.rules.ExpectedException;
8+
9+
import java.math.BigInteger;
10+
import java.math.RoundingMode;
11+
12+
import static org.hamcrest.core.IsEqual.equalTo;
13+
import static org.junit.Assert.assertFalse;
14+
import static org.junit.Assert.assertThat;
15+
import static org.junit.Assert.assertTrue;
16+
17+
public class GuavaMathTest {
18+
@Test
19+
public void testIntMathAdd() {
20+
try {
21+
IntMath.checkedAdd(Integer.MAX_VALUE, 1);
22+
assertTrue(false);
23+
} catch (ArithmeticException e) {
24+
assertTrue(true);
25+
}
26+
27+
try {
28+
IntMath.checkedAdd(Integer.MIN_VALUE, -1);
29+
assertTrue(false);
30+
} catch (ArithmeticException e) {
31+
assertTrue(true);
32+
}
33+
34+
int result1 = IntMath.checkedAdd(2, 1);
35+
assertThat(result1, equalTo(3));
36+
37+
int result2 = IntMath.saturatedAdd(Integer.MAX_VALUE, 100);
38+
assertThat(result2, equalTo(Integer.MAX_VALUE));
39+
40+
int result3 = IntMath.saturatedAdd(Integer.MIN_VALUE, -100);
41+
assertThat(result3, equalTo(Integer.MIN_VALUE));
42+
}
43+
44+
@Test
45+
public void testIntMathSubtract() {
46+
try {
47+
IntMath.checkedSubtract(Integer.MIN_VALUE, 1);
48+
assertTrue(false);
49+
} catch (ArithmeticException e) {
50+
assertTrue(true);
51+
}
52+
53+
try {
54+
IntMath.checkedSubtract(Integer.MAX_VALUE, -1);
55+
assertTrue(false);
56+
} catch (ArithmeticException e) {
57+
assertTrue(true);
58+
};
59+
60+
int result1 = IntMath.checkedSubtract(200, 100);
61+
assertThat(result1, equalTo(100));
62+
63+
int result2 = IntMath.saturatedSubtract(Integer.MIN_VALUE, 1);
64+
assertThat(result2, equalTo(Integer.MIN_VALUE));
65+
66+
int result3 = IntMath.saturatedSubtract(Integer.MAX_VALUE, -1);
67+
assertThat(result3, equalTo(Integer.MAX_VALUE));
68+
}
69+
70+
@Test
71+
public void testIntMathMultiply() {
72+
try {
73+
IntMath.checkedMultiply(Integer.MAX_VALUE, 2);
74+
assertTrue(false);
75+
} catch (ArithmeticException e) {
76+
assertTrue(true);
77+
}
78+
79+
try {
80+
IntMath.checkedMultiply(Integer.MIN_VALUE, 2);
81+
assertTrue(false);
82+
} catch (ArithmeticException e) {
83+
assertTrue(true);
84+
}
85+
86+
int result1 = IntMath.checkedMultiply(21, 3);
87+
assertThat(result1, equalTo(63));
88+
89+
int result2 = IntMath.saturatedMultiply(Integer.MAX_VALUE, 2);
90+
assertThat(result2, equalTo(Integer.MAX_VALUE));
91+
92+
int result3 = IntMath.saturatedMultiply(Integer.MIN_VALUE, 2);
93+
assertThat(result3, equalTo(Integer.MIN_VALUE));
94+
}
95+
96+
@Test
97+
public void testIntMathPow() {
98+
try {
99+
IntMath.checkedPow(Integer.MAX_VALUE, 2);
100+
assertTrue(false);
101+
} catch (ArithmeticException e) {
102+
assertTrue(true);
103+
}
104+
105+
try {
106+
IntMath.checkedPow(Integer.MIN_VALUE, 3);
107+
assertTrue(false);
108+
} catch (ArithmeticException e) {
109+
assertTrue(true);
110+
}
111+
112+
int result1 = IntMath.saturatedPow(3, 3);
113+
assertThat(result1, equalTo(27));
114+
115+
int result2 = IntMath.saturatedPow(Integer.MAX_VALUE, 2);
116+
assertThat(result2, equalTo(Integer.MAX_VALUE));
117+
118+
int result3 = IntMath.saturatedPow(Integer.MIN_VALUE, 3);
119+
assertThat(result3, equalTo(Integer.MIN_VALUE));
120+
}
121+
122+
@Test
123+
public void testIntMathRound() {
124+
int result1 = IntMath.divide(3, 2, RoundingMode.DOWN);
125+
assertThat(result1, equalTo(1));
126+
int result2 = IntMath.divide(3, 2, RoundingMode.UP);
127+
assertThat(result2, equalTo(2));
128+
129+
int result3 = IntMath.log2(5, RoundingMode.FLOOR);
130+
assertThat(result3, equalTo(2));
131+
int result4 = IntMath.log2(5, RoundingMode.CEILING);
132+
assertThat(result4, equalTo(3));
133+
134+
int result5 = IntMath.log10(11, RoundingMode.HALF_UP);
135+
assertThat(result5, equalTo(1));
136+
137+
int result6 = IntMath.sqrt(4, RoundingMode.UNNECESSARY);
138+
assertThat(result6, equalTo(2));
139+
try {
140+
IntMath.sqrt(5, RoundingMode.UNNECESSARY);
141+
assertTrue(false);
142+
} catch (ArithmeticException e) {
143+
assertTrue(true);
144+
}
145+
}
146+
147+
@Test
148+
public void testIntMathAdditionalFunctions() {
149+
int result1 = IntMath.gcd(15, 20);
150+
assertThat(result1, equalTo(5));
151+
152+
int result2 = IntMath.mod(8, 3);
153+
assertThat(result2, equalTo(2));
154+
155+
boolean result3 = IntMath.isPowerOfTwo(8);
156+
assertTrue(result3);
157+
boolean result4 = IntMath.isPowerOfTwo(9);
158+
assertFalse(result4);
159+
160+
int result5 = IntMath.factorial(4);
161+
assertThat(result5, equalTo(24));
162+
163+
int result6 = IntMath.binomial(7, 3);
164+
assertThat(result6, equalTo(35));
165+
}
166+
167+
@Test
168+
public void should_detect_integer() {
169+
boolean result1 = DoubleMath.isMathematicalInteger(2.0);
170+
assertThat(result1, equalTo(true));
171+
boolean result2 = DoubleMath.isMathematicalInteger(2.1);
172+
assertThat(result2, equalTo(false));
173+
}
174+
175+
@Test
176+
public void should_round_to_integer_types() {
177+
int result3 = DoubleMath.roundToInt(2.5, RoundingMode.DOWN);
178+
assertThat(result3, equalTo(2));
179+
180+
long result4 = DoubleMath.roundToLong(2.5, RoundingMode.HALF_UP);
181+
assertThat(result4, equalTo(3L));
182+
183+
BigInteger result5 = DoubleMath.roundToBigInteger(2.5, RoundingMode.UP);
184+
assertThat(result5, equalTo(new BigInteger("3")));
185+
}
186+
187+
@Test
188+
public void should_calculate_log_2() {
189+
int result6 = DoubleMath.log2(10, RoundingMode.UP);
190+
assertThat(result6, equalTo(4));
191+
}
192+
}

0 commit comments

Comments
 (0)