Compare dictionary

Hi

I am trying to set up unit tests for my project and I'm wondering about comparing objects...

This works...

  var inputData = {
                "data"=>[
                          {"distance"=>5"title"=>"Distance is 5"}, 
                          {"distance"=>3"title"=>"Distance is 3"},
                        ]
              };
  var expectedData = inputData;
  var actualData = inputData;
  Test.assert(actualData.equals(expectedData));
  Test.assert(actualData == expectedData);

This works...

  var inputData = {
                "data"=>[
                          {"distance"=>5"title"=>"Distance is 5"}, 
                          {"distance"=>3"title"=>"Distance is 3"},
                        ]
              };
  var expectedData = inputData;
  var actualData = inputData;
  Test.assert(actualData.equals(expectedData));
  Test.assert(actualData == expectedData);
This does not....
  var inputData = {
                "data"=>[
                          {"distance"=>5"title"=>"Distance is 5"}, 
                          {"distance"=>3"title"=>"Distance is 3"},
                        ]
              };
  var expectedData = {
                "data"=>[
                          {"distance"=>5"title"=>"Distance is 5"}, 
                          {"distance"=>3"title"=>"Distance is 3"},
                        ]
              };

  Test.assert(expectedData.equals(inputData));
  Test.assert(expectedData == inputData);
Why not?
Thanks
Gary
  • Dictionaries are incredibly expensive (memory-wise) in CIQ's resource-constrained environment

    Afaik memory limits do not apply to unit tests

  • Sure, which is why I was agreeing with the general advice not to use dictionaries. If you are unit testing dictionaries, then I'm guessing you are using them in your code.

    Of course in many cases they're unavoidable, but I just find them to be extremely memory-heavy, and I've saved a lot of memory in both data fields and full device apps by refactoring dictionaries to be something less efficient time-wise (like a "structured" lookup table implemented with a flat array), but far more efficient memory-wise.

  • If you are unit testing dictionaries, then I'm guessing you are using them in your code

    I would expect the exact opposite if you only need this code only for your unit tests.

  • Thanks all, I'm just getting started with connectIQ so have plenty to learn about memory usage. At the moment, I am downloading data from a JSON REST api so it arrives as a dictionary. It's a relatively simple data set and there is no reason I couldn't restructure into a flat array if that makes better use of the available memory. For now, while I'm prototyping the app, I'll stick with the dictionary but will certainly look to optimise.

    The code above will be used as the foundation of my unit tests - the approach allows each test to be described in words as in the simple example below. The verifyValueEquals() is a deepEqual that enables a generic approach without worrying about the data types at the test level.

    (:test)
    function aQuickTest(logger)
    {
    	var riteway = new ritewayTest();
    
    	var x = 1;
      var y = 1;
    
    	return riteway.assert(logger, {
    		"Given"=>"two new numbers",
    		"Should"=>"check they are equal",
    		"Actual"=>x,
    		"Expected"=>y
    	});
    }