this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "Python.h"
3
4#include "capi-fixture.h"
5#include "capi-testing.h"
6
7namespace py {
8namespace testing {
9using PyhashExtensionApiTest = ExtensionApi;
10
11TEST_F(PyhashExtensionApiTest, PyHashPointerReturnsHash) {
12 // We currently use the same hash algorithm as cpython so we can test for a
13 // specific result. This is optional and we can change the test when we want
14 // to use a different hashing algorithm.
15 if (sizeof(void*) == 8) {
16 Py_hash_t result =
17 _Py_HashPointer(reinterpret_cast<void*>(0xcafebabebadf00d));
18 EXPECT_EQ(result, static_cast<Py_hash_t>(0xd0cafebabebadf00));
19 }
20}
21
22TEST_F(PyhashExtensionApiTest, _Py_HashDoubleReturnsHash) {
23 PyRun_SimpleString(R"(
24hash_value = hash(-42.42)
25)");
26 PyObjectPtr hash_value(mainModuleGet("hash_value"));
27 Py_hash_t result = _Py_HashDouble(-42.42);
28 EXPECT_TRUE(isLongEqualsLong(hash_value, result));
29}
30
31TEST_F(PyhashExtensionApiTest, _Py_HashBytesWithSmallBytesReturnsHash) {
32 PyRun_SimpleString(R"(
33hash_value = hash(b"jo")
34)");
35 PyObjectPtr hash_value(mainModuleGet("hash_value"));
36 Py_hash_t result = _Py_HashBytes("jo", 2);
37 EXPECT_TRUE(isLongEqualsLong(hash_value, result));
38}
39
40TEST_F(PyhashExtensionApiTest, _Py_HashBytesWithLargeBytesReturnsHash) {
41 PyRun_SimpleString(R"(
42hash_value = hash(b"Monty Python")
43)");
44 PyObjectPtr hash_value(mainModuleGet("hash_value"));
45 Py_hash_t result = _Py_HashBytes("Monty Python", 12);
46 EXPECT_TRUE(isLongEqualsLong(hash_value, result));
47}
48
49TEST_F(PyhashExtensionApiTest, _Py_HashSecretReturnsHashSecret) {
50 EXPECT_EQ(_Py_HashSecret.fnv.prefix,
51 static_cast<Py_hash_t>(_Py_HashSecret.siphash.k0));
52 EXPECT_EQ(_Py_HashSecret.fnv.suffix,
53 static_cast<Py_hash_t>(_Py_HashSecret.siphash.k1));
54 EXPECT_EQ(_Py_HashSecret.djbx33a.suffix, _Py_HashSecret.expat.hashsalt);
55}
56
57} // namespace testing
58} // namespace py