+/*
+ * 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_jhash_32b 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_jhash_32b((const unaligned_uint32_t *)key,
+ hashtest_key_lens[i] >> 2,
+ hashtest_initvals[j]);
+ if (hash != hash32) {
+ printf("rte_jhash returns different value (0x%x)"
+ "than rte_jhash_32b (0x%x)\n",
+ hash, hash32);
+ return -1;
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * Verify that rte_jhash and rte_jhash_1word, rte_jhash_2words
+ * and rte_jhash_3words return the same
+ */
+static int
+verify_jhash_words(void)
+{
+ unsigned i;
+ uint32_t key[3];
+ uint32_t hash, hash_words;
+
+ for (i = 0; i < 3; i++)
+ key[i] = rand();
+
+ /* Test rte_jhash_1word */
+ hash = rte_jhash(key, 4, 0);
+ hash_words = rte_jhash_1word(key[0], 0);
+ if (hash != hash_words) {
+ printf("rte_jhash returns different value (0x%x)"
+ "than rte_jhash_1word (0x%x)\n",
+ hash, hash_words);
+ return -1;
+ }
+ /* Test rte_jhash_2words */
+ hash = rte_jhash(key, 8, 0);
+ hash_words = rte_jhash_2words(key[0], key[1], 0);
+ if (hash != hash_words) {
+ printf("rte_jhash returns different value (0x%x)"
+ "than rte_jhash_2words (0x%x)\n",
+ hash, hash_words);
+ return -1;
+ }
+ /* Test rte_jhash_3words */
+ hash = rte_jhash(key, 12, 0);
+ hash_words = rte_jhash_3words(key[0], key[1], key[2], 0);
+ if (hash != hash_words) {
+ printf("rte_jhash returns different value (0x%x)"
+ "than rte_jhash_3words (0x%x)\n",
+ hash, hash_words);
+ 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;
+
+ if (verify_jhash_words() != 0)
+ return -1;
+
+ return 0;
+
+}
+