Skip to main content
blog title image

4 minute read - examples Java For Testers

Let's Code - Binary Chopifier - Just Enough Code and Tooling to Start

Dec 5, 2016

TLDR; “Let’s code a Binary Chopifier” which I plan, code as prototype to plan in an @Test method, test interactively, experiment, and release as an @Test.

I want to create a few more examples of “Java in Action” and I’m doing that in some YouTube videos and blog posts that I think of as “Let’s Code”. First up is “Let’s code a Binary Chopifier” which I plan, prototype to plan, test interactively, experiment, and release to Github.

Let’s code a Binary Chopifier

When I was recording - Let’s Explore Google Search I made a note to write a binary chopifier.

In this series of videos we are going to create the binary chopifier and add it to Test Tool Hub.

Plan

First thing I did was make notes on what I wanted to support me in testing:

        Tool idea: binary chopper!
        start: 1024 end: 2048
        result
        chop: value (inc)
        -------------------
        01: 1536  (512)
        02: 1792 (256)
        03: 1920 (128)
        04: 1984 (64)
        05: 2016 (32)
        06: 2032 (16)
        07: 2040 (8)
        08: 2044 (4)
        09: 2046 (2)
        10: 2047 (1)
        11: 2048 (0)

Explaining Binary Chop:

  • I try a value of length 2048
  • System doesn’t accept it because it is too long
  • I want to find the limit
  • I try 1024 (I binary chop 2048) and if that is accepted then
  • I try 1536 (midway between 1024 and 2048), and if that is accepted then
  • etc. until I narrow down on the value that is the limit

And if you watch the video you’ll see my mental arithmetic process was quite slow. I could spend the time boosting my mental arithmetic, or I could write a tool to help me.

Guess which is easier?

So I write a tool.

Thinking through an algorithm

The plan above represents a basic output to support me as the tester.

Really all I want is the chop and the value, but I used inc to help me calculate the chops

  • So I calculate the difference between the start and end: 1024
  • Divide it by 2 (chop) to get 512 then I add that to start (inc) and get 1536
  • And keep going.

Start by writing a ‘@Test’ which does this

I start by writing an @Test method which implements this algorithm and I can see if it works or not

@Test
public void calculateBinaryChopForStartAndEndFromThoughtAlgorithm(){

  int start = 1024;
  int end = 2048;
  int choppoint=start;
  int inc = start;

  while(inc > 0){

  inc = (end-choppoint)/2;
  choppoint=choppoint+inc;
  System.out.println(String.format("%d (%d)", choppoint, inc));
  }

}

Which gives me the output

1536 (512)
1792 (256)
1920 (128)
1984 (64)
2016 (32)
2032 (16)
2040 (8)
2044 (4)
2046 (2)
2047 (1)
2047 (0)

Which isn’t what I was looking for, but makes sense since on the last increment it is zero.
Perhaps then, inc isn’t inc it is diff between end and chop point.

So rather than ‘add to’ the start, I should ’take away’ from the end

    @Test
    public void calculateBinaryChopForStartAndEnd(){

        int start = 1024;
        int end = 2048;
        int choppoint=start;
        int inc = start;

        while(inc > 0){

            inc = (end-choppoint)/2;
            choppoint=end-inc;
            System.out.println(String.format("%d (%d)", choppoint, inc));
        }

    }

Which gives me my original plan:

1536 (512)
1792 (256)
1920 (128)
1984 (64)
2016 (32)
2032 (16)
2040 (8)
2044 (4)
2046 (2)
2047 (1)
2048 (0)

But since I’m working from the end, I’m wondering if what I actually do is just keep halfing the difference:

@Test
public void calculateBinaryChopForStartAndEndHalfDifference(){

  int start = 1024;
  int end = 2048;
  int diff = end-start;

  while(diff > 0){
  diff = diff/2;
  System.out.println(String.format("%d (%d)", end-diff, diff));
  }
}

Which gives me:

1536 (512)
1792 (256)
1920 (128)
1984 (64)
2016 (32)
2032 (16)
2040 (8)
2044 (4)
2046 (2)
2047 (1)
2048 (0)

And is much simpler.

And since this ’test’ is a useful ’tool’ for me - I’ll stop there for this video. And next I’ll start refactoring this out into a library for binary chopping so that I can then use that in the Test Tool Hub.