Functional Programming in Java, Second Edition: Functional Programming in Java, Second Edition: JUnit code improvements for Chapter 11, pages 184 ff “Refactoring More Complex loops”

The same remarks apply as for “Refactoring the Traditional for Loop”
- No
init()
- Classes under test implement a common interface that is called in tests.
- Negative tests which apply for input less than 1900, resulting in exceptions.
package chapter11;
import org.junit.jupiter.api.Test;
import java.time.Year;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.*;
// Everything is inside the MoreComplexLoopTest class for convenience.
// Documentation for java.time.Year.isLeap()
// https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#isLeap--
public class MoreComplexLoopTest {
interface LeapYears {
int countFrom1900(int upTo);
}
static class LeapYearCountBefore implements LeapYears {
public int countFrom1900(int upTo) {
if (upTo < 1900) {
throw new IllegalArgumentException("The passed value is < 1900: " + upTo);
}
int count = 0;
for (int year = 1900; year <= upTo; year += 4) {
if (Year.isLeap(year)) {
count++;
}
}
return count;
}
}
static class LeapYearCountAfter implements LeapYears {
public int countFrom1900(int upTo) {
if (upTo < 1900) {
throw new IllegalArgumentException("The passed value is < 1900: " + upTo);
}
return (int) IntStream.iterate(1900, year -> year <= upTo, year -> year + 4)
.filter(Year::isLeap)
.count();
}
}
private static void commonLeapYearsTests(final LeapYears leapYears) {
assertAll(
() -> assertEquals(0, leapYears.countFrom1900(1900)),
() -> assertEquals(25, leapYears.countFrom1900(2000)),
() -> assertEquals(27, leapYears.countFrom1900(2010)),
() -> assertEquals(31, leapYears.countFrom1900(2025)),
() -> assertEquals(49, leapYears.countFrom1900(2100)),
() -> assertEquals(267, leapYears.countFrom1900(3000)),
() -> assertThrows(IllegalArgumentException.class, () -> leapYears.countFrom1900(1800))
);
}
@Test
void leapYearCountBefore() {
commonLeapYearsTests(new LeapYearCountBefore());
}
@Test
void leapYearCountAfter() {
commonLeapYearsTests(new LeapYearCountAfter());
}
}
Popular Pragprog topics

This test is broken right out of the box…
— FAIL: TestAgent (7.82s)
agent_test.go:77:
Error Trace: agent_test.go:77
agent_test.go:...
New

Whilst the author has been careful to provide exact results for the tests elsewhere in the book (such as surds with the transformation te...
New

Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84:
bundle exec sequel...
New

Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks”
While running the smoke tests in Chapter 2, I get these...
New

Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New

Hi,
build fails on:
bracket-lib = “~0.8.1”
when running on Mac Mini M1 Rust version 1.5.0:
Compiling winit v0.22.2
error[E0308]: mi...
New

A Common-Sense Guide to Data Structures and Algorithms, Second Edition by Jay Wengrow @jaywengrow
Hi,
I have the paperback version of t...
New

Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New

AWDWR 7, page 152, page 153:
Hello everyone,
I’m a little bit lost on the hotwire part. I didn’t fully understand it.
On page 152 @rub...
New

I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1:
programming_ML/code/03_...
New
Other popular topics

New

I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New

SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New

Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New

I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl:
The Ser...
New

This looks like a stunning keycap set :orange_heart:
A LEGENDARY KEYBOARD LIVES ON
When you bought an Apple Macintosh computer in the e...
New

“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin
Even when you’re ultimately right about what the future ho...
New

Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New

A Hero’s Journey with Chris Pine
Chris Pine, author of Learn to Program, Third Edition, discusses his journey to becoming a Pragm...
New

Biggest jackpot ever apparently! :upside_down_face:
I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New
Latest in Pragprog
Latest (all)
Categories:
My Saved Portals
-
None saved yet
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /security
- /django
- /nodejs
- /centos
- /haskell
- /rails
- /fable
- /gleam
- /swift
- /js
- /deno
- /assemblyscript
- /tailwind
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /preact
- /flutter
- /c-plus-plus
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /nim
- /vuejs
- /emacs
- /elm
- /nerves