Home > Net >  Implementation based on test Java (bytewise) JUnit test
Implementation based on test Java (bytewise) JUnit test

Time:11-23

I'm trying to implement a class in order to pass the following test (Using bytewise operators & and |

public void hasFlagTest1() {
    byte resource = ResourceUtil.getFlag(FLAG_PUBLIC_SECURITY, FLAG_PRIVATE_SECURITY, FLAG_BASIC_LIFE_SUPPORT);

    Assert.assertTrue(ResourceUtil.hasPublicSecurity(resource));
    Assert.assertTrue(ResourceUtil.hasPrivateSecurity(resource));
    Assert.assertTrue(ResourceUtil.hasBasicLifeSupport(resource));
    Assert.assertFalse(ResourceUtil.hasVolunteers(resource));
    Assert.assertFalse(ResourceUtil.hasAllOpts(resource));
}

The constant values that are passed by parameters are

public static final byte FLAG_PRIVATE_SECURITY = 1;
public static final byte FLAG_PUBLIC_SECURITY = 2;
public static final byte FLAG_BASIC_LIFE_SUPPORT = 4;
public static final byte FLAG_VOLUNTEERS = 8;
public static final byte FLAG_ALL_OPTS  = 15;

I have already created a class but not sure how to implement all methods in order to make this test pass, here's the class:

public class ResourceUtil {

public static byte getFlag(byte arg, byte arg2, byte arg3){
    
    return result;  //just a value, not implemented
}; 

public static boolean hasPublicSecurity(byte resource) {

    return true;  //just a value, not implemented
}

public static boolean hasPrivateSecurity(byte resource) {

    return true;  //just a value, not implemented
}

public static boolean hasBasicLifeSupport(byte resource) {

    return true;  //just a value, not implemented
}

public static boolean hasVolunteers(byte resource) {
    return true;  //just a value, not implemented
}

public static boolean hasAllOpts(byte resource) {

    return true;  //just a value, not implemented
}

}

Any idea? I've been trying some

CodePudding user response:

It looks like you are just looking for set bits inside a byte (and for all set bits in that last method). To do this you can simply use your flags as a mask and compare to the flag.

public class ResourceUtil {

    public static boolean hasPublicSecurity(byte resource) {
        return resource & FLAG_PUBLIC_SECURITY == FLAG_PUBLIC_SECURITY;
    }

    public static boolean hasPrivateSecurity(byte resource) {
        return resource & FLAG_PRIVATE_SECURITY == FLAG_PRIVATE_SECURITY;
    }

    public static boolean hasBasicLifeSupport(byte resource) {
        return resource & FLAG_BASIC_LIFE_SUPPORT == FLAG_BASIC_LIFE_SUPPORT;
    }

    public static boolean hasVolunteers(byte resource) {
        return resource & FLAG_VOLUNTEERS == FLAG_VOLUNTEERS;
    }

    public static boolean hasAllOpts(byte resource) {
        return resource & FLAG_ALL_OPTS == FLAG_ALL_OPTS;
    }

}

NOTE: It's not completely clear to me what the getFlags method is supposed to accomplish, but these checks should work

CodePudding user response:

The best (and probably fastest in terms of performance) way to do it would be to create a logical AND operation between the resources that you receive and the location of the bit.

So something like this:

public static boolean hasPublicSecurity(byte resource) {
    return (resource & FLAG_PUBLIC_SECURITY) == FLAG_PUBLIC_SECURITY;
}

public static boolean hasPrivateSecurity(byte resource) {
    return (resource & FLAG_PRIVATE_SECURITY) = FLAG_PRIVATE_SECURITY;
}

public static boolean hasBasicLifeSupport(byte resource) {
    return (resource & FLAG_BASIC_LIFE_SUPPORT) == FLAG_BASIC_LIFE_SUPPORT;
}

public static boolean hasVolunteers(byte resource) {
    return (resource & FLAG_VOLUNTEERS) == FLAG_VOLUNTEERS;
}

public static boolean hasAllOpts(byte resource) {
    return (resource & FLAG_ALL_OPTS) == FLAG_ALL_OPTS;
}

The reason for the code is as follows, let's assume in your example that we have the value 7 (111 in binary), when we check for PUBLIC_SECURITY we're basically checking if:

111 & 001 == 001

If the result is zero, that means that the relevant bit was not 1.

  • Related