dtonhofer

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 topics Top

Razor54672
The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New
ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
herminiotorres
Hi! I know not the intentions behind this narrative when called, on page XI: mount() |&gt; handle_event() |&gt; render() but the correc...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
jskubick
I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint. As entered, the SwitchCompat changes color to hol...
New
Charles
In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “&gt;...
New
ggerico
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
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New

Other popular topics Top

dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
AstonJ
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
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
New
Maartz
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
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New

Latest in PragProg

View all threads ❯