Skip to content

Commit

Permalink
Add tests for float/double attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple committed Dec 1, 2021
1 parent f81c357 commit 79e3a9e
Show file tree
Hide file tree
Showing 6 changed files with 5,551 additions and 3,240 deletions.
1 change: 1 addition & 0 deletions examples/chip-tool/templates/tests-commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <commands/tests/TestCommand.h>
#include <commands/common/CommandInvoker.h>
#include <math.h> // For INFINITY

class TestList : public Command
{
Expand Down
236 changes: 236 additions & 0 deletions src/app/tests/suites/TestCluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,118 @@ tests:
response:
value: 0

# Tests for float attribute

- label: "Read attribute SINGLE Default Value"
command: "readAttribute"
attribute: "float_single"
response:
value: 0

- label: "Write attribute SINGLE medium Value"
command: "writeAttribute"
attribute: "float_single"
arguments:
value: 0.1

- label: "Read attribute SINGLE medium Value"
command: "readAttribute"
attribute: "float_single"
response:
value: 0.1

- label: "Write attribute SINGLE large Value"
command: "writeAttribute"
attribute: "float_single"
arguments:
value: 1.7e10

- label: "Read attribute SINGLE large Value"
command: "readAttribute"
attribute: "float_single"
response:
value: 1.7e10

- label: "Write attribute SINGLE small Value"
command: "writeAttribute"
attribute: "float_single"
arguments:
value: 1.7e-10

- label: "Read attribute SINGLE small Value"
command: "readAttribute"
attribute: "float_single"
response:
value: 1.7e-10

- label: "Write attribute SINGLE Default Value"
command: "writeAttribute"
attribute: "float_single"
arguments:
value: 0

- label: "Read attribute SINGLE Default Value"
command: "readAttribute"
attribute: "float_single"
response:
value: 0

# Tests for double attribute

- label: "Read attribute DOUBLE Default Value"
command: "readAttribute"
attribute: "float_double"
response:
value: 0

- label: "Write attribute DOUBLE medium Value"
command: "writeAttribute"
attribute: "float_double"
arguments:
value: 0.1234567890123

- label: "Read attribute DOUBLE medium Value"
command: "readAttribute"
attribute: "float_double"
response:
value: 0.1234567890123

- label: "Write attribute DOUBLE large Value"
command: "writeAttribute"
attribute: "float_double"
arguments:
value: 1.7e200

- label: "Read attribute DOUBLE large Value"
command: "readAttribute"
attribute: "float_double"
response:
value: 1.7e200

- label: "Write attribute DOUBLE small Value"
command: "writeAttribute"
attribute: "float_double"
arguments:
value: 1.7e-200

- label: "Read attribute DOUBLE small Value"
command: "readAttribute"
attribute: "float_double"
response:
value: 1.7e-200

- label: "Write attribute DOUBLE Default Value"
command: "writeAttribute"
attribute: "float_double"
arguments:
value: 0

- label: "Read attribute DOUBLE Default Value"
command: "readAttribute"
attribute: "float_double"
response:
value: 0

# Tests for Enum8 attribute

- label: "Read attribute ENUM8 Default Value"
Expand Down Expand Up @@ -1941,6 +2053,130 @@ tests:
response:
value: null

# Tests for float attribute

- label: "Write attribute NULLABLE_SINGLE medium Value"
command: "writeAttribute"
attribute: "nullable_float_single"
arguments:
value: 0.1

- label: "Read attribute NULLABLE_SINGLE medium Value"
command: "readAttribute"
attribute: "nullable_float_single"
response:
value: 0.1

- label: "Write attribute NULLABLE_SINGLE largest Value"
command: "writeAttribute"
attribute: "nullable_float_single"
arguments:
value: Infinity

- label: "Read attribute NULLABLE_SINGLE largest Value"
command: "readAttribute"
attribute: "nullable_float_single"
response:
value: Infinity

- label: "Write attribute NULLABLE_SINGLE smallest Value"
command: "writeAttribute"
attribute: "nullable_float_single"
arguments:
value: -Infinity

- label: "Read attribute NULLABLE_SINGLE smallest Value"
command: "readAttribute"
attribute: "nullable_float_single"
response:
value: -Infinity

- label: "Write attribute NULLABLE_SINGLE null Value"
command: "writeAttribute"
attribute: "nullable_float_single"
arguments:
value: null

- label: "Read attribute NULLABLE_SINGLE null Value"
command: "readAttribute"
attribute: "nullable_float_single"
response:
value: null

- label: "Write attribute NULLABLE_SINGLE 0 Value"
command: "writeAttribute"
attribute: "nullable_float_single"
arguments:
value: 0

- label: "Read attribute NULLABLE_SINGLE 0 Value"
command: "readAttribute"
attribute: "nullable_float_single"
response:
value: 0

# Tests for double attribute

- label: "Write attribute NULLABLE_DOUBLE medium Value"
command: "writeAttribute"
attribute: "nullable_float_double"
arguments:
value: 0.1234567890123

- label: "Read attribute NULLABLE_DOUBLE medium Value"
command: "readAttribute"
attribute: "nullable_float_double"
response:
value: 0.1234567890123

- label: "Write attribute NULLABLE_DOUBLE largest Value"
command: "writeAttribute"
attribute: "nullable_float_double"
arguments:
value: Infinity

- label: "Read attribute NULLABLE_DOUBLE largest Value"
command: "readAttribute"
attribute: "nullable_float_double"
response:
value: Infinity

- label: "Write attribute NULLABLE_DOUBLE smallest Value"
command: "writeAttribute"
attribute: "nullable_float_double"
arguments:
value: -Infinity

- label: "Read attribute NULLABLE_DOUBLE smallest Value"
command: "readAttribute"
attribute: "nullable_float_double"
response:
value: -Infinity

- label: "Write attribute NULLABLE_DOUBLE null Value"
command: "writeAttribute"
attribute: "nullable_float_double"
arguments:
value: null

- label: "Read attribute NULLABLE_DOUBLE null Value"
command: "readAttribute"
attribute: "nullable_float_double"
response:
value: null

- label: "Write attribute NULLABLE_DOUBLE 0 Value"
command: "writeAttribute"
attribute: "nullable_float_double"
arguments:
value: 0

- label: "Read attribute NULLABLE_DOUBLE 0 Value"
command: "readAttribute"
attribute: "nullable_float_double"
response:
value: 0

# Tests for Enum8 attribute

- label: "Write attribute NULLABLE_ENUM8 Max Value"
Expand Down
19 changes: 16 additions & 3 deletions src/app/zap-templates/templates/app/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,26 @@ function asTypedLiteral(value, type)
case 'uint64_t':
return value + (valueIsANumber ? 'ULL' : '');
case 'float':
if (!valueIsANumber || value == 0) {
// "0f" is not a valid value, so don't output that; just leave it
// as "0".
if (!valueIsANumber) {
return value;
}
if (value == Infinity || value == -Infinity) {
return `${(value < 0) ? '-' : ''}INFINITY`
}
// If the number looks like an integer, append ".0" to the end;
// otherwise adding an "f" suffix makes compilers complain.
value = value.toString();
if (value.match(/^[0-9]+$/)) {
value = value + ".0";
}
return value + 'f';
default:
if (!valueIsANumber) {
return value;
}
if (value == Infinity || value == -Infinity) {
return `${(value < 0) ? '-' : ''}INFINITY`
}
return value;
}
})
Expand Down
2 changes: 2 additions & 0 deletions src/darwin/Framework/CHIP/templates/clusters-tests.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#import <app/util/af-enums.h>

#import <math.h> // For INFINITY

// system dependencies
#import <XCTest/XCTest.h>

Expand Down
Loading

0 comments on commit 79e3a9e

Please sign in to comment.