zeek icon indicating copy to clipboard operation
zeek copied to clipboard

`DictIterator` does not honor `DictOrder::ORDERED` mode

Open timwoj opened this issue 3 years ago • 0 comments

I added this unit test to Dict.cc:

TEST_CASE("dict ordered iteration")
	{
	PDict<uint32_t> dict(DictOrder::ORDERED);

	// These key values are specifically contrived to be inserted
	// into the dictionary in a different order by default.
	uint32_t val = 15;
	uint32_t key_val = 5;
	auto key = std::make_unique<detail::HashKey>(key_val);

	uint32_t val2 = 10;
	uint32_t key_val2 = 25;
	auto key2 = std::make_unique<detail::HashKey>(key_val2);

	uint32_t val3 = 30;
	uint32_t key_val3 = 45;
	auto key3 = std::make_unique<detail::HashKey>(key_val3);

	dict.Insert(key.get(), &val);
	dict.Insert(key2.get(), &val2);
	dict.Insert(key3.get(), &val3);

	int count = 0;

	for ( const auto& entry : dict )
		{
		auto* v = static_cast<uint32_t*>(entry.value);
		uint32_t k = *(uint32_t*)entry.GetKey();

		// The keys should be returned in the same order we inserted
		// them, which is 5, 25, 45.
		if ( count == 0 )
			CHECK(k == 5);
		else if ( count == 1 )
			CHECK(k == 25);
		else if ( count == 2 )
			CHECK(k == 45);

		count++;
		}
	}

It returns the keys in the order that would be expected if the dictionary was in UNORDERED mode and the test fails. This affects being able to loop using ranged-for loops in C++ as well as for statements in script-land.

timwoj avatar Sep 01 '22 00:09 timwoj