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
As per the title, thanks.
New
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks”
While running the smoke tests in Chapter 2, I get these...
New
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159
When I execute the command:
user=> (create-...
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
Hi @venkats,
It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
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
A Common-Sense Guide to Data Structures and Algorithms, Second Edition by Jay Wengrow @jaywengrow
Hi,
I have the paperback version of t...
New
I ran this command after installing the sample application:
$ cards add do something --owner Brian
And got a file not found error:
Fil...
New
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error.
lib/pento_web/live/product_l...
New
Getting an error when installing the dependencies at the start of this chapter:
could not compile dependency :exla, "mix compile" failed...
New
Other popular topics
What chair do you have while working… and why?
Is there a ‘best’ type of chair or working position for developers?
New
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
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
Hi folks,
I don’t know if I saw this here but, here’s a new programming language, called Roc
Reminds me a bit of Elm and thus Haskell. ...
New
Author Spotlight
Rebecca Skinner
@RebeccaSkinner
Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
Big O Notation can make your code faster by orders of magnitude. Get the hands-on info you need to master data structures and algorithms ...
New
Develop, deploy, and debug BEAM applications using BEAMOps: a new paradigm that focuses on scalability, fault tolerance, and owning each ...
New
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New
A concise guide to MySQL 9 database administration, covering fundamental concepts, techniques, and best practices.
Neil Smyth
MySQL...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /python
- /rails
- /js
- /security
- /go
- /swift
- /vim
- /clojure
- /haskell
- /emacs
- /java
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /html
- /opensuse
- /centos
- /php
- /zig
- /deepseek
- /scala
- /sublime-text
- /textmate
- /lisp
- /react-native
- /nixos
- /debian
- /agda
- /kubuntu
- /arch-linux
- /deno
- /django
- /revery
- /ubuntu
- /nodejs
- /manjaro
- /spring
- /diversity
- /lua
- /julia
- /c
- /slackware







