【问题标题】:Implementation based on test Java (bytewise) JUnit test基于测试的实现 Java (bytewise) JUnit测试
【发布时间】:2022-11-23 02:50:29
【问题描述】:

我正在尝试实现一个类以传递以下内容测试(使用字节运算符 & 和 |

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));
}

这常数值传递的参数是

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;

我已经创建了一个班级但不确定如何实现所有方法以使该测试通过,这是类:

公共类 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
}

}

任何想法?我一直在尝试一些

【问题讨论】:

    标签: java testing junit byte implementation


    【解决方案1】:

    看起来您只是在寻找一个字节内的设置位(以及最后一种方法中的所有设置位)。为此,您可以简单地使用您的标志作为掩码并与标志进行比较。

    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;
        }
    
    }
    

    注意:我不完全清楚 getFlags 方法应该完成什么,但这些检查应该有效

    【讨论】:

    • 嗨,谢谢你! getFlags 方法也没有实现,只是我尝试过的方法,因为在测试中,它使用它
    【解决方案2】:

    最好的(并且在性能方面可能是最快的)方法是在您收到的资源和位的位置之间创建一个逻辑 AND 操作。

    所以像这样:

    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;
    }
    

    代码的原因如下,假设在您的示例中我们有值 7(111 二进制),当我们检查 PUBLIC_SECURITY 时,我们基本上是在检查:

    111 & 001 == 001
    

    如果结果为零,则意味着相关位不是 1。

    【讨论】:

    • hasAllOpts的这个方法肯定不行...
    • 是的,你是对的 - 最好检查它是否是完全预期的标志 - 否则你可能有边缘情况 - 将更新 :)
    • 哦,我只是注意到这正是您的解决方案 - 我错过了您发布的内容 :sweat_smile:
    猜你喜欢
    • 2020-10-19
    • 2010-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    相关资源
    最近更新 更多