예제 #1
0
bool keyDataEqual (const Key & k1, const Key & k2)
{
	if (!k1 || !k2) return false;

	if (k1.isBinary () != k2.isBinary ()) return false;

	if (k1.isBinary () && k2.isBinary ())
	{
		return k1.getBinary () == k2.getBinary ();
	}
	else
	{
		return k1.getString () == k2.getString ();
	}

	return true;
}
예제 #2
0
파일: test.cpp 프로젝트: mpranj/libelektra
void TestCommand::doBinaryTest ()
{
	vector<string> teststrings;
	teststrings.push_back ("binary value");
	teststrings.push_back ("binary value with null");
	teststrings.back ().push_back ('\0');

	teststrings.push_back ("binary value with null");
	teststrings.back ().push_back ('\0');
	teststrings.back () += "in the middle";

	teststrings.push_back (" a very long value with many spaces and basically very very long, but only binary text ... ");
	for (int i = 0; i < 256; ++i)
	{
		teststrings.back ().push_back ('\0');
		teststrings.back () += " very very long, but only binary text ... ";
	}

	teststrings.push_back ("all chars:");
	for (int i = 0; i < 256; ++i)
		teststrings.back ().push_back (i);
	teststrings.push_back ("€");
	for (int i = 0; i < 256; ++i)
	{
		string s;
		s.push_back (i);
		teststrings.push_back (s);
	}


	for (auto & teststring : teststrings)
	{
		{
			KDB kdb;
			Key t = root.dup ();
			t.addBaseName ("binary");
			t.setBinary (teststring.c_str (), teststring.length ());

			KeySet basic;
			basic.append (t);

			KeySet test;
			kdb.get (test, root);
			kdb.set (basic, root);
		}

		{
			KDB kdb;

			KeySet test;
			kdb.get (test, root);

			Key t = root.dup ();
			t.addBaseName ("binary");

			Key res = test.lookup (t);
			nrTest++;
			if (!res)
			{
				nrError++;
				cerr << "Binary test failed (key not found)" << t.getName () << endl;
				continue;
			}

			nrTest++;
			if (res.getBinarySize () > 0 && static_cast<size_t> (res.getBinarySize ()) != teststring.length ())
			{
				nrError++;
				cerr << "Binary test failed (length is not equal)" << endl;
				cerr << "We got: \"" << res.getBinary () << "\"" << endl;
				cerr << "We wanted: \"" << teststring << "\"" << endl;
			}

			nrTest++;
			if (res.getBinary () != teststring)
			{
				nrError++;
				cerr << "Binary test failed (value is not equal)" << endl;
				cerr << "We got: \"" << res.getBinary () << "\"" << endl;
				cerr << "We wanted: \"" << teststring << "\"" << endl;
			}
		}
	}
}