Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to test this? Binary digits with 4 positions

Consider 4 input fields A, B, C and D on a web surface. The user can fill any of these arbitrary. There are 16 combinations of how to fill these fields. The ones allowed are:

A B C D
-------
1 0 0 0
1 1 0 0
1 1 1 0
1 1 1 1

where 1 means not null and 0 means null.

I am using the MVC pattern with jsf. I don't want the logic to be in the view, but rather in the controller. What is the best way to check this in Java?

I implemented two solutions so far:

Solution 1:

@Override
public boolean isInputInvalid(Integer a, Integer b, Integer c, Integer d) {
    if (isNotSet(a) && isNotSet(b) && isNotSet(c) && isNotSet(d) {
        return true;
    }
    return (firstParameterDoesNotExistAndSecondDoesExist(a, b)) || (firstParameterDoesNotExistAndSecondDoesExist(b, c)) || (firstParameterDoesNotExistAndSecondDoesExist(c, d));
}

private boolean firstParameterDoesNotExistAndSecondDoesExist(Integer firstParameter, Integer secondParameter) {
    return isNotSet(firstParameter) && !isNotSet(secondParameter);
}

private boolean isNotSet(Integer parameter) {
    return parameter == null;
}

Solution 2:

public boolean isInputValid(Integer a, Integer b, Integer c, Integer d) {
    if (exists(a) && !exists(b) && !exists(c) && !exists(d) || //
            exists(a) && exists(b) && !exists(c) && !exists(d) || //
            exists(a) && exists(b) && exists(c) && !exists(d) || //
            exists(a) && exists(b) && exists(c) && exists(d)) {
        return true;
    }
    return false;
}

private boolean exists(Integer level) {
    return level != null;
}

Note:

The first methods checks if input is invalid, while the second checks if input is valid (note the names of the methods).

I wrote 16 unit test cases, which all run green with both versions.

Do you have any hints/tips/tricks on how to get the code even more readable?

like image 428
Chris311 Avatar asked Oct 07 '15 15:10

Chris311


People also ask

How do you find the value of 4 in binary?

Solution: 4 in binary is (100)2. Here, 4 is represented in the decimal number system, where we can represent the number using the digits from 0-9. However, in a binary number system, we use only two digits, such as 0 and 1.

What are 4 binary digits called?

In computer parlance, one binary digit is called a bit, two digits are called a crumb, four digits are called a nibble, and eight digits are called a byte.

How many values can you represent with 4 binary digits?

There are 16 values from 0-15 that can be represented using 4 digit binary.


1 Answers

Valid combinations are: 1000, 1100, 1110 and 1111

If you only care about readability:

public static List<String> validOptions = Arrays.asList("1000","1100","1110","1111");

public boolean isValid(Integer a, Integer b, Integer c, Integer d)
{
    StringBuilder sb = new StringBuilder();
    sb.append(a==null ? 0 : 1); 
    sb.append(b==null ? 0 : 1), 
    sb.append(c==null ? 0 : 1); 
    sb.append(d==null ? 0 : 1);
    return validOptions.contains(sb.toString());
}

Note that this is not the fastest or cleanest solution (wastes some CPU and memory)

like image 145
Rob Audenaerde Avatar answered Oct 10 '22 18:10

Rob Audenaerde