Complete
over 3 years ago

Not a CIQ issue. For more context, here's a link to the Bug Reports FAQ, https://forums.garmin.com/developer/connect-iq/w/wiki/5/bug-reports-faq.

Next time, consider starting a discussion. A discussion can be escalated to a bug report.

Unable to test km to mile conversion of half marathon

```

  (:distance)
  module Distance {

    function toMiles(dist) {
      var mi = dist.toDouble() * 0.6213711922d;
      return mi.toDouble();
    }

  }

I have the following Test:

// t.is() == test.assertEqualmessage
// t.ok() == test.assertMessage()
// t.pass() == test.assertMessage(true, msg)
// t.fail() == test.assertMessage(false, msg)

    (:test)
    function testDistance(logger) {

      t.is(dist.toMiles(1), 0.6213711922d, "1km is 0.6213711922 miles");

      var mi = dist.toMiles(21.0975);
      Sys.println(mi); // prints 13.109379

      var mile = 13.109379d;
      Sys.println(mile); // print 13.109379

      if (mile == mi) {
        t.pass("A mile is a mile");
      }
      else {
        t.fail("A mile isn't a mile");
      }

      t.is(dist.toMiles(21.0975), 13.109378728d, "Half marathon distance");
      t.ok(dist.toMiles(21.0975) == 13.109379d, "Half marathon distance");

      return true;
    }

This fails on Exception: ASSERTION FAILED: A mile isn't a mile    File: UnitTests

I'm kinda expecting it all to work.


The problem is because I cannot see what the test sees (as described here https://forums.garmin.com/developer/connect-iq/i/bug-reports/test-assertequalmessage-and-friends-to-show-incorrectness), so from there I'm sitting the values should be correct. Working around it by adding a toString() doesn't work, it still thinks it isn't the same value.

  • 1. always have the most possible but at least the necessary data in your error messages. i.e: t.fail("A mile (" + mile + ") isn't a mile (" + mi + ")")
    2. it can happen that even here you will see the "same" value and still == won't be true. This is life and how floating point arithmetic works.
    3. you can maybe convert to string and compare them as string to fix that.
    4. IMHO you don't need return mi.toDouble();, enough return mi, 'cause it's anyway already a double.