Skip to content

Latest commit

 

History

History
118 lines (101 loc) · 3.05 KB

mpi_unittest.cc

File metadata and controls

118 lines (101 loc) · 3.05 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
#include "secdert.h"
#include "secitem.h"
#include "secport.h"
#include "gtest/gtest.h"
#include <stdint.h>
#include <string.h>
#include <string>
Nov 17, 2016
Nov 17, 2016
15
16
17
18
19
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
20
21
22
#include "mpi.h"
namespace nss_test {
Nov 17, 2016
Nov 17, 2016
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void gettime(struct timespec *tp) {
#ifdef __MACH__
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
tp->tv_sec = mts.tv_sec;
tp->tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_MONOTONIC, tp);
#endif
}
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class MPITest : public ::testing::Test {
protected:
void TestCmp(const std::string a_string, const std::string b_string,
int result) {
mp_int a, b, c;
MP_DIGITS(&a) = 0;
MP_DIGITS(&b) = 0;
MP_DIGITS(&c) = 0;
ASSERT_EQ(MP_OKAY, mp_init(&a));
ASSERT_EQ(MP_OKAY, mp_init(&b));
mp_read_radix(&a, a_string.c_str(), 16);
mp_read_radix(&b, b_string.c_str(), 16);
EXPECT_EQ(result, mp_cmp(&a, &b));
}
};
TEST_F(MPITest, MpiCmp01Test) { TestCmp("0", "1", -1); }
TEST_F(MPITest, MpiCmp10Test) { TestCmp("1", "0", 1); }
TEST_F(MPITest, MpiCmp00Test) { TestCmp("0", "0", 0); }
TEST_F(MPITest, MpiCmp11Test) { TestCmp("1", "1", 0); }
TEST_F(MPITest, MpiCmpConstTest) {
mp_int a, b, c;
MP_DIGITS(&a) = 0;
MP_DIGITS(&b) = 0;
MP_DIGITS(&c) = 0;
ASSERT_EQ(MP_OKAY, mp_init(&a));
ASSERT_EQ(MP_OKAY, mp_init(&b));
ASSERT_EQ(MP_OKAY, mp_init(&c));
Nov 17, 2016
Nov 17, 2016
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
mp_read_radix(
&a,
const_cast<char *>(
"FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"),
16);
mp_read_radix(
&b,
const_cast<char *>(
"FF0FFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"),
16);
mp_read_radix(
&c,
const_cast<char *>(
"FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632550"),
16);
85
86
87
88
89
90
91
92
mp_taint(&b);
mp_taint(&c);
uint32_t runs = 5000000;
uint32_t time_b = 0, time_c = 0;
for (uint32_t i = 0; i < runs; ++i) {
struct timespec start, end;
Nov 17, 2016
Nov 17, 2016
93
gettime(&start);
94
int r = mp_cmp(&a, &b);
Nov 17, 2016
Nov 17, 2016
95
gettime(&end);
96
unsigned long long used = end.tv_sec * 1000000000L + end.tv_nsec;
Nov 17, 2016
Nov 17, 2016
97
98
used -= static_cast<unsigned long long>(start.tv_sec * 1000000000L +
start.tv_nsec);
99
100
101
102
103
104
105
time_b += used;
ASSERT_EQ(1, r);
}
printf("time b: %u\n", time_b / runs);
for (uint32_t i = 0; i < runs; ++i) {
struct timespec start, end;
Nov 17, 2016
Nov 17, 2016
106
gettime(&start);
107
int r = mp_cmp(&a, &c);
Nov 17, 2016
Nov 17, 2016
108
gettime(&end);
109
unsigned long long used = end.tv_sec * 1000000000L + end.tv_nsec;
Nov 17, 2016
Nov 17, 2016
110
111
used -= static_cast<unsigned long long>(start.tv_sec * 1000000000L +
start.tv_nsec);
112
113
114
115
116
117
118
time_c += used;
ASSERT_EQ(1, r);
}
printf("time c: %u\n", time_c / runs);
}
} // nss_test