diff --git a/cpp-package/example/test_kvstore.cpp b/cpp-package/example/test_kvstore.cpp index bb81e3a3031a..f0fb8beb6719 100644 --- a/cpp-package/example/test_kvstore.cpp +++ b/cpp-package/example/test_kvstore.cpp @@ -20,9 +20,7 @@ using namespace mxnet::cpp; -static int test_single_key() { - int ret = 0; - +static bool test_single_key() { std::string key = "singlekeytest"; NDArray result(Shape(4), Context::cpu()); @@ -38,10 +36,12 @@ static int test_single_key() { // compare for (size_t j=0; j < result.Size(); j++) { - ret += (result.GetData()[j] == data.GetData()[j]) ? 0 : 1; + if (result.GetData()[j] != data.GetData()[j]) { + LG << "Error: wrong initialized data in singlekeytest, expect " + << data.GetData()[j] << " got " << result.GetData()[j]; + return false; + } } - if (ret != 0) - return ret; // push gradient NDArray grad({0.1f, -2.f, -4.4f, 0.f}, Shape(4), Context::cpu()); @@ -54,15 +54,17 @@ static int test_single_key() { // compare for (size_t j=0; j < result.Size(); j++) { - ret += (result.GetData()[j] == grad.GetData()[j]) ? 0 : 1; + if (result.GetData()[j] == grad.GetData()[j]) { + LG << "Error: wrong gradient data in singlekeytest, expect " + << grad.GetData()[j] << " got " << result.GetData()[j]; + return false; + } } - return ret; + return true; } -static int test_multiple_key() { - int ret = 0; - +static bool test_multiple_key() { std::vector keys(2); keys[0] = "multikeytest-0"; keys[1] = "multikeytest-1"; @@ -72,10 +74,10 @@ static int test_multiple_key() { results[1] = NDArray(Shape(4), Context::cpu()); // initialize data - std::vector datas(2); - datas[0] = NDArray({0.f, 2.f, -3.12f, 4.f}, Shape(4), Context::cpu()); - datas[1] = NDArray({0.8f, -2.f, 6.6f, 77.f}, Shape(4), Context::cpu()); - KVStore::Init(keys, datas); + std::vector data(2); + data[0] = NDArray({0.f, 2.f, -3.12f, 4.f}, Shape(4), Context::cpu()); + data[1] = NDArray({0.8f, -2.f, 6.6f, 77.f}, Shape(4), Context::cpu()); + KVStore::Init(keys, data); NDArray::WaitAll(); // retrieve result @@ -85,11 +87,13 @@ static int test_multiple_key() { // compare for (size_t i=0; i < results.size(); i++) { for (size_t j=0; j < results[i].Size(); j++) { - ret += (results[i].GetData()[j] == datas[i].GetData()[j]) ? 0 : 1; + if (results[i].GetData()[j] == data[i].GetData()[j]) { + LG << "Error: wrong initialized data in multikeytest, expect " + << data[i].GetData()[j] << " got " << results[i].GetData()[j]; + return false; + } } } - if (ret != 0) - return ret; // push gradient, reduce for the second std::vector push_keys(3); @@ -110,22 +114,31 @@ static int test_multiple_key() { // compare the first for (size_t j=0; j < results[0].Size(); j++) { - ret += (results[0].GetData()[j] == grads[0].GetData()[j]) ? 0 : 1; + if (results[0].GetData()[j] == grads[0].GetData()[j]) { + LG << "Error: wrong gradient data, expect " << grads[0].GetData()[j] + << " got " << result[0].GetData()[j]; + return false; + } } // compare the second for (size_t j=0; j < results[1].Size(); j++) { - ret += (results[1].GetData()[j] == (grads[1].GetData()[j] + grads[2].GetData()[j])) ? 0 : 1; + if (results[1].GetData()[j] == (grads[1].GetData()[j] + grads[2].GetData()[j])) { + LG << "Error: wrong reduced gradient data, expect " + << (grads[1].GetData()[j] + grads[2].GetData()[j]) + << " got " << result[1].GetData()[j]; + return false; + } } - return ret; + return true; } int main(int argc, char** argv) { KVStore::SetType("local"); - int ret1 = test_single_key(); - int ret2 = test_multiple_key(); + bool ret1 = test_single_key(); + bool ret2 = test_multiple_key(); MXNotifyShutdown(); return ret1 + ret2;