在当前文件夹中的一个文件中,创建名为 HasSameSizeAs 的类,该类派生自 matlab.unittest.constraints.BooleanConstraint 类。类构造函数接受预期值,该预期值的大小会与实际值的大小进行比较。预期值存储在 ValueWithExpectedSize 属性中。建议的做法是使 BooleanConstraint 实现不可变,因此将属性 SetAccess 特性设置为 immutable。

classdef HasSameSizeAs < matlab.unittest.constraints.BooleanConstraint

properties(SetAccess = immutable)

ValueWithExpectedSize

end

methods

function constraint = HasSameSizeAs(value)

constraint.ValueWithExpectedSize = value;

end

end

end

在具有 private 访问权限的 methods 块中,定义辅助方法 sizeMatchesExpected,该方法确定实际值和预期值是否具有相同的大小。该方法由其他约束方法调用。

methods(Access = private)

function bool = sizeMatchesExpected(constraint,actual)

bool = isequal(size(actual),size(constraint.ValueWithExpectedSize));

end

end

matlab.unittest.constraints.BooleanConstraint 类是 matlab.unittest.constraints.Constraint 类的子类。因此,从 BooleanConstraint 类派生的类必须覆盖 Constraint 类的方法。在 methods 块内,覆盖 satisfiedBy 和 getDiagnosticFor 方法。satisfiedBy 实现必须包含比较逻辑并返回一个逻辑值。getDiagnosticFor 实现必须针对约束对实际值进行计算,并提供 Diagnostic 对象。在此示例中,getDiagnosticFor 返回 StringDiagnostic 对象。

methods

function bool = satisfiedBy(constraint,actual)

bool = constraint.sizeMatchesExpected(actual);

end

function diag = getDiagnosticFor(constraint,actual)

import matlab.unittest.diagnostics.StringDiagnostic

if constraint.sizeMatchesExpected(actual)

diag = StringDiagnostic('HasSameSizeAs passed.');

else

diag = StringDiagnostic(sprintf(...

'HasSameSizeAs failed.\nActual Size: [%s]\nExpectedSize: [%s]',...

int2str(size(actual)),...

int2str(size(constraint.ValueWithExpectedSize))));

end

end

end

派生自 BooleanConstraint 的类必须实现 getNegativeDiagnosticFor 方法。当约束取反时,此方法必须提供 Diagnostic 对象。

在具有 protected 访问权限的 methods 块中覆盖 getNegativeDiagnosticFor。

methods(Access = protected)

function diag = getNegativeDiagnosticFor(constraint,actual)

import matlab.unittest.diagnostics.StringDiagnostic

if constraint.sizeMatchesExpected(actual)

diag = StringDiagnostic(sprintf(...

['Negated HasSameSizeAs failed.\nSize [%s] of '...

'Actual Value and Expected Value were the same '...

'but should not have been.'],int2str(size(actual))));

else

diag = StringDiagnostic('Negated HasSameSizeAs passed.');

end

end

end

为实现必需方法,约束继承相应的 and、or 和 not 重载,以便与其他 BooleanConstraint 对象进行组合或求反。

HasSameSizeAs 类定义

这是 HasSameSizeAs 类的完整代码。

classdef HasSameSizeAs < matlab.unittest.constraints.BooleanConstraint

properties(SetAccess = immutable)

ValueWithExpectedSize

end

methods

function constraint = HasSameSizeAs(value)

constraint.ValueWithExpectedSize = value;

end

function bool = satisfiedBy(constraint,actual)

bool = constraint.sizeMatchesExpected(actual);

end

function diag = getDiagnosticFor(constraint,actual)

import matlab.unittest.diagnostics.StringDiagnostic

if constraint.sizeMatchesExpected(actual)

diag = StringDiagnostic('HasSameSizeAs passed.');

else

diag = StringDiagnostic(sprintf(...

'HasSameSizeAs failed.\nActual Size: [%s]\nExpectedSize: [%s]',...

int2str(size(actual)),...

int2str(size(constraint.ValueWithExpectedSize))));

end

end

end

methods(Access = protected)

function diag = getNegativeDiagnosticFor(constraint,actual)

import matlab.unittest.diagnostics.StringDiagnostic

if constraint.sizeMatchesExpected(actual)

diag = StringDiagnostic(sprintf(...

['Negated HasSameSizeAs failed.\nSize [%s] of '...

'Actual Value and Expected Value were the same '...

'but should not have been.'],int2str(size(actual))));

else

diag = StringDiagnostic('Negated HasSameSizeAs passed.');

end

end

end

methods(Access = private)

function bool = sizeMatchesExpected(constraint,actual)

bool = isequal(size(actual),size(constraint.ValueWithExpectedSize));

end

end

end

测试预期大小

在命令提示符处,创建测试用例以执行交互式测试。

import matlab.unittest.TestCase

import matlab.unittest.constraints.HasLength

testCase = TestCase.forInteractiveUse;

测试一个通过的用例。测试通过,原因是 or 条件之一 HasLength(5) 为 true。

testCase.verifyThat(zeros(5),HasLength(5) | ~HasSameSizeAs(repmat(1,5)))

Verification passed.

测试一个失败的用例。测试失败,原因是 and 条件之一 ~HasSameSizeAs(repmat(1,5)) 为 false。

testCase.verifyThat(zeros(5),HasLength(5) & ~HasSameSizeAs(repmat(1,5)))

Verification failed.

---------------------

Framework Diagnostic:

---------------------

AndConstraint failed.

--> + [First Condition]:

| HasLength passed.

|

| Actual Value:

| 0 0 0 0 0

| 0 0 0 0 0

| 0 0 0 0 0

| 0 0 0 0 0

| 0 0 0 0 0

| Expected Length:

| 5

--> AND

+ [Second Condition]:

| Negated HasSameSizeAs failed.

| Size [5 5] of Actual Value and Expected Value were the same but should not have been.

-+---------------------

matlab 定义布尔变量,创建自定义布尔约束相关推荐

  1. [ASP.NET MVC2 系列] ASP.NET MVC 之如何创建自定义路由约束

     [ASP.NET MVC2 系列]      [ASP.NET MVC2 系列] ASP.Net MVC教程之<在15分钟内用ASP.Net MVC创建一个电影数据库应用程序>      ...

  2. e0312 不存在用户定义的_VistaPro创建自定义变量

    IESVE软件中VistaPro模块中的结果变量众多,很多用户反应变量数据不容易获得,其实在此模块中用户可以根据现有变量(天气数据.房间设置和负荷.建筑负荷.能源.碳排放)进行组合,利用VE软件自带的 ...

  3. matlab定义循环变量

    popsize=10 for i=1:popsize if i==1 order1=(1:popsize); else eval(['order',num2str(i),'=randperm(pops ...

  4. matlab persistent静态变量

    persistent关键字说明: matlab中persistent定义为一个静态变量,程序结束静态变量内存不释放,只有在matlab程序关闭时,静态变量的内存才被清除.当程序结束时,不希望变量的值被 ...

  5. sql定义布尔类型变量_Php –布尔变量类型,True,False

    sql定义布尔类型变量 [rps-include post=6522] [rps-include post = 6522] Programming generally consist of numer ...

  6. c语言布尔变量定义,C语言布尔类型介绍 C语言标准(C89)没有定义布尔类型,如果你使用true...

    10的叠乘值为 3628800 输出结果为: //导入 stdbool.h 来使用布尔类型#include #include //计算n!,n的值在main中定义int main(void){ int ...

  7. js 定义布尔变量不能写成 boolean flag;

    js 定义布尔变量不能写成 boolean flag; js 没有 boolean 类,得写成 var flag;

  8. c语言布尔变量如何定义,关于布尔变量的用法(新手)

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 /* * ======================================================================== ...

  9. c51语言定义布尔变量,51单片机指令系统:布尔变量操作指令(9)

    51单片机指令系统:布尔变量操作指令(9) 第二十七课:布尔变量操作指令分析 布尔处理功能是MCS-51系列单片机的一个重要特征,这是出于实际应用需要而设置的.布尔变量也即开关变量,它是以位(bit) ...

最新文章

  1. 职场残酷!美国科技巨头 “花式劝退” 老员工,数万人丢掉饭碗
  2. linux shell 文件去除重复行
  3. 006_Buzz属性和组
  4. 为文档快速插入页眉和页脚
  5. android menu菜单 实现点击后不消失_教你修改Windows万年不变的黄色文件夹,让文件也不枯燥...
  6. c++ cdi+示例_C ++中带有示例的本地类
  7. iptables高级应用实例
  8. Java并发编程之安全发布对象的四种方法
  9. 小波与小波包、小波包分解与信号重构、小波包能量特征提取
  10. 解决“找不到msvcr120.dll,需要重新安装服务 ”最终版本
  11. 极速PDF打开文件后工具栏不显示怎么办
  12. maven-resources-plugin:2.6:resources (default-resources)
  13. php随机点名代码怎么做,html座位表随机点名的实例代码
  14. 绘制奥林匹克标志——利用python turtle画奥运五环
  15. 夏季养生要以“清”为贵
  16. error C1083:无法打开文件stddef.h或crtd bg.h
  17. 公共计算机课件,全国计算机等级考试二级ACCESS公共基础知识课件.ppt
  18. 后LHC时代对撞机:瞬间输出能量超全球电力千倍
  19. 2022-2028年中国针织行业生产现状分析与投资前景战略研究报告
  20. Android2023暑期实习---网易游戏一面面经

热门文章

  1. python打包一直出错_PyInstaller打包出错问题解决实践记录
  2. Redis 大量数据插入
  3. 基于python的小论文_基于Python的网上论坛的实现
  4. 常见Java反射的应用
  5. c语言孪生素数用函数,孪生素数(C语言),C语言编程。。孪生素数
  6. Edge浏览器打开初始页总是跳转2345怎么办?
  7. UE4源码加密及资源加密
  8. JudgeOnline
  9. ECharts基本使用
  10. mac系统安装nacos,详细图文教程,解决启动不了nacos