Logical not operator does not behave correctly for Number and Long types
o The Environment:
Windows 7
Eclipse Neon.2 (4.6.2)
ConnectIQ 2.2.3
o A detailed description of the issue
MonkeyC allows the logical not operator (!) to be applied to Boolean, Number and Long. It behaves as expected with the Boolean type, but seems to behave incorrectly with the other types. In other languages that allow logical not to be applied to an integral type negating a non-zero value results in a zero, and negating zero results in some non-zero value.
Further investigation indicates that the logical not operator is being implemented in terms of the bitwise complement, instead of the logical operators.
o Steps to reproduce the issue
Build and run the test code provided as a unit test (build and run with -t).
o Any applicable additional information
N/A
o A code sample that can reproduce the issue (in email only if preferred)
using Toybox.Application as App;
using Toybox.System as Sys;
using Toybox.Lang as Lang;
function do_test_logical_not_operator(v, logger) {
var i = 0;
if (v) {
++i;
}
if (!v) {
++i;
}
var s = Lang.format("v=$1$ !v=$2$", [ v, !v ]);
if (i != 1) {
logger.error(s);
return 1;
}
return 0;
}
(:test) function test_logical_not_boolean(logger) {
var failures = 0;
failures += do_test_logical_not_operator(false, logger);
failures += do_test_logical_not_operator( true, logger);
return failures == 0;
}
(:test) function test_logical_not_number(logger) {
var failures = 0;
failures += do_test_logical_not_operator(-2, logger);
failures += do_test_logical_not_operator(-1, logger);
failures += do_test_logical_not_operator( 0, logger);
failures += do_test_logical_not_operator( 1, logger);
failures += do_test_logical_not_operator( 2, logger);
return failures == 0;
}
(:test) function test_logical_not_long(logger) {
var failures = 0;
failures += do_test_logical_not_operator(-2l, logger);
failures += do_test_logical_not_operator(-1l, logger);
failures += do_test_logical_not_operator( 0l, logger);
failures += do_test_logical_not_operator( 1l, logger);
failures += do_test_logical_not_operator( 2l, logger);
return failures == 0;
}
class XApp extends App.AppBase
{
function initialize() {
AppBase.initialize();
}
function getInitialView() {
return null;
}
}
The results...
Found Transport: tcp
Connecting...
Connecting to device...
Device Version 0.1.0
Device id 1 name "A garmin device"
Shell Version 0.1.0
------------------------------------------------------------------------------
Executing test test_logical_not_boolean...
PASS
------------------------------------------------------------------------------
Executing test test_logical_not_number...
ERROR (19:57): v=-2 !v=1
ERROR (19:57): v=1 !v=-2
ERROR (19:57): v=2 !v=-3
FAIL
------------------------------------------------------------------------------
Executing test test_logical_not_long...
ERROR (19:57): v=-2 !v=1
ERROR (19:57): v=1 !v=-2
ERROR (19:57): v=2 !v=-3
FAIL
==============================================================================
RESULTS
Test: Status:
test_logical_not_boolean PASS
test_logical_not_number FAIL
test_logical_not_long FAIL
Ran 3 tests
FAILED (failures=2, errors=0)
Connection Finished
Closing shell and port
Travis