1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include <gtest/gtest.h>
21 #include <mxnet/base.h>
22 
23 using namespace mxnet;
24 using namespace std;
25 
26 /*
27  * Test that different Context have different hash values
28  */
TEST(ContextHashTest,ContextHashUnique)29 TEST(ContextHashTest, ContextHashUnique) {
30     set<size_t> hashes;
31     size_t collision_count = 0;
32     size_t total = 0;
33     for (size_t dev_type = 0; dev_type < 32; ++dev_type) {
34         for (size_t dev_id = 0; dev_id < 64; ++dev_id) {
35             auto ctx = Context::Create(static_cast<Context::DeviceType>(dev_type), dev_id);
36             size_t res = std::hash<Context>()(ctx);
37             auto insert_res = hashes.insert(res);
38             if (!insert_res.second)
39                 ++collision_count;
40             ++total;
41         }
42     }
43     double collision = collision_count / static_cast<double>(total);
44     cout << "mxnet::Context std::hash collision ratio: " << collision << endl;
45     EXPECT_LE(collision, 0.04);
46 }
47