
dtonhofer
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 Pragmatic Bookshelf topics

page 20: … protoc command…
I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New

In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New

Hi everyone!
There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New

Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display)
It looks like
.with_simple_console_no_bg(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
New

Hi Jamis,
I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019.
This test doesn’t pass for me:
...
New

Title: Hands-on Rust: question about get_component (page 295)
(feel free to respond. “You dug you’re own hole… good luck”)
I have somet...
New

I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New

In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions:
In Gemfile, change
gem 'rails'
t...
New

I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
New

@parrt
In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New
Other popular topics

Reading something? Working on something? Planning something? Changing jobs even!?
If you’re up for sharing, please let us know what you’...
New

What chair do you have while working… and why?
Is there a ‘best’ type of chair or working position for developers?
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

Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New

You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New

Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New

The V Programming Language
Simple language for building maintainable programs
V is already mentioned couple of times in the forum, but I...
New

Was just curious to see if any were around, found this one:
I got 51/100:
Not sure if it was meant to buy I am sure at times the b...
New

Programming Ruby is the most complete book on Ruby, covering both the language itself and the standard library as well as commonly used t...
New

Fight complexity and reclaim the original spirit of agility by learning to simplify how you develop software. The result: a more humane a...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /emacs
- /clojure
- /haskell
- /java
- /onivim
- /typescript
- /svelte
- /crystal
- /kotlin
- /c-plus-plus
- /tailwind
- /gleam
- /react
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /html
- /opensuse
- /centos
- /php
- /deepseek
- /zig
- /scala
- /sublime-text
- /textmate
- /lisp
- /nixos
- /debian
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /django
- /ubuntu
- /revery
- /spring
- /manjaro
- /nodejs
- /diversity
- /lua
- /julia
- /slackware
- /c
- /neovim