Skip to content

Commit 9cce703

Browse files
committed
refactor: move 'gettime_i64()' to tests_common.h
Relocate the clock time getter to tests_common.h to make it easily reusable across test programs. This will be useful for the upcoming unit test framework. Context - why not placing it inside testutil.h?: The bench program links against the production-compiled library, not its own compiled version. Therefore, `gettime_i64()` cannot be moved to testutil.h, because testutil.h calls `secp256k1_pubkey_save()`, which exists only in the internal secp256k1.c and not in the public API.
1 parent 03fb60a commit 9cce703

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ noinst_HEADERS += src/precomputed_ecmult.h
4545
noinst_HEADERS += src/precomputed_ecmult_gen.h
4646
noinst_HEADERS += src/assumptions.h
4747
noinst_HEADERS += src/checkmem.h
48+
noinst_HEADERS += src/tests_common.h
4849
noinst_HEADERS += src/testutil.h
4950
noinst_HEADERS += src/util.h
5051
noinst_HEADERS += src/util_local_visibility.h

src/bench.h

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,7 @@
1212
#include <stdio.h>
1313
#include <string.h>
1414

15-
#if (defined(_MSC_VER) && _MSC_VER >= 1900)
16-
# include <time.h>
17-
#else
18-
# include <sys/time.h>
19-
#endif
20-
21-
static int64_t gettime_i64(void) {
22-
#if (defined(_MSC_VER) && _MSC_VER >= 1900)
23-
/* C11 way to get wallclock time */
24-
struct timespec tv;
25-
if (!timespec_get(&tv, TIME_UTC)) {
26-
fputs("timespec_get failed!", stderr);
27-
exit(EXIT_FAILURE);
28-
}
29-
return (int64_t)tv.tv_nsec / 1000 + (int64_t)tv.tv_sec * 1000000LL;
30-
#else
31-
struct timeval tv;
32-
gettimeofday(&tv, NULL);
33-
return (int64_t)tv.tv_usec + (int64_t)tv.tv_sec * 1000000LL;
34-
#endif
35-
}
15+
#include "tests_common.h"
3616

3717
#define FP_EXP (6)
3818
#define FP_MULT (1000000LL)

src/tests_common.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/***********************************************************************
2+
* Distributed under the MIT software license, see the accompanying *
3+
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
4+
***********************************************************************/
5+
6+
#ifndef SECP256K1_TESTS_COMMON_H
7+
#define SECP256K1_TESTS_COMMON_H
8+
9+
/***********************************************************************
10+
* Test Support Utilities
11+
*
12+
* This file provides general-purpose functions for tests and benchmark
13+
* programs. Unlike testutil.h, this file is not linked to the library,
14+
* allowing each program to choose whether to run against the production
15+
* API or access library internals directly.
16+
***********************************************************************/
17+
18+
#include <stdint.h>
19+
20+
#if (defined(_MSC_VER) && _MSC_VER >= 1900)
21+
# include <time.h>
22+
#else
23+
# include <sys/time.h>
24+
#endif
25+
26+
static int64_t gettime_i64(void) {
27+
#if (defined(_MSC_VER) && _MSC_VER >= 1900)
28+
/* C11 way to get wallclock time */
29+
struct timespec tv;
30+
if (!timespec_get(&tv, TIME_UTC)) {
31+
fputs("timespec_get failed!", stderr);
32+
exit(EXIT_FAILURE);
33+
}
34+
return (int64_t)tv.tv_nsec / 1000 + (int64_t)tv.tv_sec * 1000000LL;
35+
#else
36+
struct timeval tv;
37+
gettimeofday(&tv, NULL);
38+
return (int64_t)tv.tv_usec + (int64_t)tv.tv_sec * 1000000LL;
39+
#endif
40+
}
41+
42+
#endif /* SECP256K1_TESTS_COMMON_H */

0 commit comments

Comments
 (0)