Return-Path: X-Original-To: apmail-singa-commits-archive@minotaur.apache.org Delivered-To: apmail-singa-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E1CC61887F for ; Sun, 13 Sep 2015 14:08:39 +0000 (UTC) Received: (qmail 63146 invoked by uid 500); 13 Sep 2015 14:08:39 -0000 Delivered-To: apmail-singa-commits-archive@singa.apache.org Received: (qmail 63127 invoked by uid 500); 13 Sep 2015 14:08:39 -0000 Mailing-List: contact commits-help@singa.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@singa.incubator.apache.org Delivered-To: mailing list commits@singa.incubator.apache.org Received: (qmail 63118 invoked by uid 99); 13 Sep 2015 14:08:39 -0000 Received: from Unknown (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 13 Sep 2015 14:08:39 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id 62FBEC0427 for ; Sun, 13 Sep 2015 14:08:39 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.795 X-Spam-Level: X-Spam-Status: No, score=0.795 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RP_MATCHES_RCVD=-0.006, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-east.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id CvaDwAECOOvq for ; Sun, 13 Sep 2015 14:08:24 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-east.apache.org (ASF Mail Server at mx1-us-east.apache.org) with SMTP id 90CA544574 for ; Sun, 13 Sep 2015 14:08:23 +0000 (UTC) Received: (qmail 63031 invoked by uid 99); 13 Sep 2015 14:08:23 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 13 Sep 2015 14:08:23 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id F1FFBDFE7C; Sun, 13 Sep 2015 14:08:22 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: wangwei@apache.org To: commits@singa.incubator.apache.org Message-Id: <397c958e6ceb4934a411795e053de31d@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: incubator-singa git commit: SINGA-64 Add the test module for utils/common Date: Sun, 13 Sep 2015 14:08:22 +0000 (UTC) Repository: incubator-singa Updated Branches: refs/heads/master 5d7d53f52 -> 077d3804f SINGA-64 Add the test module for utils/common add test code for functions in utils/common Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/077d3804 Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/077d3804 Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/077d3804 Branch: refs/heads/master Commit: 077d3804fd0bf2d710b13b13ae76f03e439438b5 Parents: 5d7d53f Author: ijingo Authored: Thu Aug 27 21:23:55 2015 +0800 Committer: ijingo Committed: Sun Sep 13 21:47:51 2015 +0800 ---------------------------------------------------------------------- Makefile.am | 1 + src/test/test_common.cc | 113 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/077d3804/Makefile.am ---------------------------------------------------------------------- diff --git a/Makefile.am b/Makefile.am index baff947..6f3be4c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -85,6 +85,7 @@ GTEST_SRCS := include/gtest/gtest-all.cc GTEST_HRDS := include/gtest/gtest.h TEST_SRCS := include/gtest/gtest_main.cc \ src/test/test_cluster.cc \ + src/test/test_common.cc \ src/test/test_msg.cc \ src/test/test_neuralnet.cc \ src/test/test_paramslicer.cc \ http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/077d3804/src/test/test_common.cc ---------------------------------------------------------------------- diff --git a/src/test/test_common.cc b/src/test/test_common.cc new file mode 100644 index 0000000..b84e860 --- /dev/null +++ b/src/test/test_common.cc @@ -0,0 +1,113 @@ +#include "gtest/gtest.h" +#include "utils/common.h" +#include +#include +#include + +using std::string; +using std::vector; +using namespace singa; + +TEST(CommonTest, TestIntVecToString) { + + vector num_vec {2, 3, 5, 7, 11}; + string str = "(2, 3, 5, 7, 11, )"; + ASSERT_EQ(str, IntVecToString(num_vec)); +} + +TEST(CommonTest, TestStringPrintf) { + const char* str_a = "abc"; + const char* str_b = "edfgh"; + const char* str_c = " !@#"; + const char* str_d = "1"; + const char* str_e = "2"; + const char* str_f = "3"; + + string fmt_a = "%s%s%s"; + string fmt_b = "[%s] [%s] [%s] "; + + string str_d_a = "abcedfgh !@#"; + string str_d_b = "[1] [2] [3] "; + + ASSERT_EQ(str_d_a, StringPrintf(fmt_a, str_a, str_b, str_c)); + ASSERT_EQ(str_d_b, StringPrintf(fmt_b, str_d, str_e, str_f)); +} + +TEST(CommonTest, TestGCDLCM) { + int a = 2, b = 5, c = 10, d = 15; + + ASSERT_EQ(1, gcd(a, b)); + ASSERT_EQ(5, gcd(c, d)); + ASSERT_EQ(10, LeastCommonMultiple(b, c)); + ASSERT_EQ(30, LeastCommonMultiple(c, d)); +} + +TEST(CommonTest, TestMetric) { + string str, msg; + Metric metric; + metric.Add("a", 0.5); + metric.Add("b", 0.5); + metric.Add("a", 1.5); + str = metric.ToLogString(); + msg = metric.ToString(); + metric.Reset(); + metric.ParseFrom(msg); + ASSERT_EQ(str, metric.ToLogString()); +} + +TEST(CommonTest, TestSlice) { + vector> slices_0; + vector sizes {14112, 96, 256, 884736, 384}; + ASSERT_EQ(slices_0, Slice(0, sizes)); + + vector> slices_1 { + { 14112 }, + { 96 }, + { 256 }, + { 884736 }, + { 384 }, + }; + + vector> slices_2 { + { 14112 }, + { 96 }, + { 256 }, + { 435328, 449408 }, + { 384 }, + }; + + vector> slices_4 { + { 14112 }, + { 96 }, + { 256 }, + { 210432,224896,224896,224512 }, + { 384 }, + }; + + vector> slices_8 { + { 14112 }, + { 96 }, + { 256 }, + { 97984,112448,112448,112448,112448,112448,112448,112064 }, + { 384 }, + }; + + ASSERT_EQ(slices_1, Slice(1, sizes)); + ASSERT_EQ(slices_2, Slice(2, sizes)); + ASSERT_EQ(slices_4, Slice(4, sizes)); + ASSERT_EQ(slices_8, Slice(8, sizes)); +} + +TEST(CommonTest, TestPartitionSlices) { + vector slices { + 97984,112448,112448,112448,112448,112448,112448,112064 + }; + vector box_1 { 0, 0, 0, 0, 0, 0, 0, 0 }; + vector box_2 { 0, 0, 0, 0, 1, 1, 1, 1 }; + vector box_4 { 0, 0, 1, 1, 2, 2, 3, 3 }; + vector box_8 { 0, 1, 2, 3, 4, 5, 6, 7 }; + ASSERT_EQ(box_1, PartitionSlices(1, slices)); + ASSERT_EQ(box_2, PartitionSlices(2, slices)); + ASSERT_EQ(box_4, PartitionSlices(4, slices)); + ASSERT_EQ(box_8, PartitionSlices(8, slices)); +}