From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by dpdk.org (Postfix) with ESMTP id E70745A4F for ; Fri, 22 May 2015 12:16:17 +0200 (CEST) Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP; 22 May 2015 03:16:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.13,474,1427785200"; d="scan'208";a="575361289" Received: from irvmail001.ir.intel.com ([163.33.26.43]) by orsmga003.jf.intel.com with ESMTP; 22 May 2015 03:16:16 -0700 Received: from sivswdev02.ir.intel.com (sivswdev02.ir.intel.com [10.237.217.46]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id t4MAGEC5008106; Fri, 22 May 2015 11:16:14 +0100 Received: from sivswdev02.ir.intel.com (localhost [127.0.0.1]) by sivswdev02.ir.intel.com with ESMTP id t4MAGEgV013089; Fri, 22 May 2015 11:16:14 +0100 Received: (from pdelarax@localhost) by sivswdev02.ir.intel.com with id t4MAGEBt013085; Fri, 22 May 2015 11:16:14 +0100 From: Pablo de Lara To: dev@dpdk.org Date: Fri, 22 May 2015 11:16:06 +0100 Message-Id: <1432289771-12799-6-git-send-email-pablo.de.lara.guarch@intel.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1432289771-12799-1-git-send-email-pablo.de.lara.guarch@intel.com> References: <1431428560-25426-1-git-send-email-pablo.de.lara.guarch@intel.com> <1432289771-12799-1-git-send-email-pablo.de.lara.guarch@intel.com> Subject: [dpdk-dev] [PATCH v5 05/10] test/hash: add new functional tests for hash functions X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 22 May 2015 10:16:19 -0000 In order to make sure that the hash functions are returning the correct values, new tests have been added: - First test compares precalculated hash values with values calculated from the existing hash functions. - Second test compares values returned from rte_jhash2 and rte_jhash, expecting same return (only for multiple of 4 bytes keys) Signed-off-by: Pablo de Lara --- app/test/test_hash_functions.c | 121 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 121 insertions(+), 0 deletions(-) diff --git a/app/test/test_hash_functions.c b/app/test/test_hash_functions.c index 3b72e8f..97a8fbd 100644 --- a/app/test/test_hash_functions.c +++ b/app/test/test_hash_functions.c @@ -47,6 +47,36 @@ #include "test.h" +/* + * Hash values calculated for key sizes from array "hashtest_key_lens" + * and for initial values from array "hashtest_initvals. + * Each key will be formed by increasing each byte by 1: + * e.g.: key size = 4, key = 0x03020100 + * key size = 8, key = 0x0706050403020100 + */ +static uint32_t hash_values_jhash[2][10] = {{ + 0x821cc2db, 0xa491f494, 0xace4cd87, 0x9e867842, + 0xd32442d6, 0x5fbafeab, 0x9cac434c, 0xecad9b0d, + 0x2dcf235e, 0xaab655d0 +}, +{ + 0xc1111b14, 0x9a95039e, 0x84f208a0, 0xfa28f3fb, + 0xfa13f7d3, 0xc7aed470, 0x74caa938, 0xa9288066, + 0xd0140735, 0xbf00519d +} +}; +static uint32_t hash_values_crc[2][10] = {{ + 0x91545164, 0x06040eb1, 0x9bb99201, 0xcc4c4fe4, + 0x14a90993, 0xf8a5dd8c, 0xc62beb31, 0x32bf340e, + 0x72f9d22b, 0x4a11475e +}, +{ + 0x98cd4c70, 0xd52c702f, 0x41fc0e1c, 0x3905f65c, + 0x94bff47f, 0x1bab102d, 0xd2911ed7, 0xe8faa813, + 0x6bea184b, 0x53028d3e +} +}; + /******************************************************************************* * Hash function performance test configuration section. Each performance test * will be performed HASHTEST_ITERATIONS times. @@ -138,9 +168,100 @@ run_hash_func_perf_tests(void) } } +/* + * Verify that hash functions return what they are expected to return + * (using precalculated values stored above) + */ +static int +verify_precalculated_hash_func_tests(void) +{ + unsigned i, j; + uint8_t key[64]; + uint32_t hash; + + for (i = 0; i < 64; i++) + key[i] = (uint8_t) i; + + for (i = 0; i < sizeof(hashtest_key_lens) / sizeof(uint32_t); i++) { + for (j = 0; j < sizeof(hashtest_initvals) / sizeof(uint32_t); j++) { + hash = rte_jhash(key, hashtest_key_lens[i], hashtest_initvals[j]); + if (hash != hash_values_jhash[j][i]) { + printf("jhash for %u bytes with initial value 0x%x." + "Expected 0x%x, but got 0x%x\n", + hashtest_key_lens[i], hashtest_initvals[j], + hash_values_jhash[j][i], hash); + return -1; + } + + hash = rte_hash_crc(key, hashtest_key_lens[i], hashtest_initvals[j]); + if (hash != hash_values_crc[j][i]) { + printf("CRC for %u bytes with initial value 0x%x." + "Expected 0x%x, but got 0x%x\n", + hashtest_key_lens[i], hashtest_initvals[j], + hash_values_crc[j][i], hash); + return -1; + } + } + } + + return 0; +} + +/* + * Verify that rte_jhash and rte_jhash2 return the same + */ +static int +verify_jhash_32bits(void) +{ + unsigned i, j; + uint8_t key[64]; + uint32_t hash, hash32; + + for (i = 0; i < 64; i++) + key[i] = rand() & 0xff; + + for (i = 0; i < sizeof(hashtest_key_lens) / sizeof(uint32_t); i++) { + for (j = 0; j < sizeof(hashtest_initvals) / sizeof(uint32_t); j++) { + /* Key size must be multiple of 4 (32 bits) */ + if ((hashtest_key_lens[i] & 0x3) == 0) { + hash = rte_jhash(key, hashtest_key_lens[i], hashtest_initvals[j]); + /* Divide key length by 4 in rte_jhash for 32 bits */ + hash32 = rte_jhash2((const uint32_t *)key, hashtest_key_lens[i] >> 2, hashtest_initvals[j]); + if (hash != hash32) { + printf("rte_jhash returns different value (0x%x)" + "than rte_jhash2 (0x%x)\n", + hash, hash32); + return -1; + } + } + } + } + + return 0; +} + +/* + * Run all functional tests for hash functions + */ +static int +run_hash_func_tests(void) +{ + if (verify_precalculated_hash_func_tests() != 0) + return -1; + + if (verify_jhash_32bits() != 0) + return -1; + + return 0; + +} + static int test_hash_functions(void) { + if (run_hash_func_tests() != 0) + return -1; + run_hash_func_perf_tests(); return 0; -- 1.7.4.1