
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

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

I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here.
...
New

On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New

The generated iex result below should list products instead of product for the metadata. (page 67)
iex> product = %Product{}
%Pento....
New

When trying to generate the protobuf .go file, I receive this error:
Unknown flag: --go_opt
libprotoc 3.12.3
MacOS 11.3.1
Googling ...
New

When I run the coverage example to report on missing lines, I get:
pytest --cov=cards --report=term-missing ch7
ERROR: usage: pytest [op...
New

When installing Cards as an editable package, I get the following error:
ERROR: File “setup.py” not found. Directory cannot be installe...
New

Hi,
I completed chapter 6 but am getting the following error when running:
thread 'main' panicked at 'Failed to load texture: IoError(O...
New

The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error.
lib/pento_web/live/product_l...
New

When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New
Other popular topics

Any thoughts on Svelte?
Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New

A PragProg Hero’s Journey with Brian P. Hogan @bphogan
Have you ever worried that your only legacy will be in the form of legacy...
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
New

My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
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

Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face:
https://on...
New

Just done a fresh install of macOS Big Sur and on installing Erlang I am getting:
asdf install erlang 23.1.2
Configure failed.
checking ...
New

Large Language Models like ChatGPT say The Darnedest Things.
The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New

Author Spotlight:
Sophie DeBenedetto
@SophieDeBenedetto
The days of the traditional request-response web application are long gone, b...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /emacs
- /haskell
- /onivim
- /svelte
- /typescript
- /crystal
- /c-plus-plus
- /kotlin
- /tailwind
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /ash
- /vscode
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /zig
- /scala
- /textmate
- /debian
- /sublime-text
- /nixos
- /lisp
- /agda
- /react-native
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /django
- /manjaro
- /spring
- /diversity
- /nodejs
- /lua
- /slackware
- /c
- /julia
- /markdown