Skip to content

Commit 81f791e

Browse files
committed
tests: add "help" command line flag and output
1 parent 5fbff5d commit 81f791e

File tree

7 files changed

+55
-13
lines changed

7 files changed

+55
-13
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ noinst_HEADERS += src/precomputed_ecmult_gen.h
4949
noinst_HEADERS += src/assumptions.h
5050
noinst_HEADERS += src/checkmem.h
5151
noinst_HEADERS += src/util.h
52+
noinst_HEADERS += src/cli_util.h
5253
noinst_HEADERS += src/int128.h
5354
noinst_HEADERS += src/int128_impl.h
5455
noinst_HEADERS += src/int128_native.h

src/bench.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "../include/secp256k1.h"
1111
#include "util.h"
12+
#include "cli_util.h"
1213
#include "bench.h"
1314

1415
static void help(int default_iters) {

src/bench.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,6 @@ static void run_benchmark(char *name, void (*benchmark)(void*, int), void (*setu
129129
printf("\n");
130130
}
131131

132-
static int have_flag(int argc, char** argv, char *flag) {
133-
char** argm = argv + argc;
134-
argv++;
135-
while (argv != argm) {
136-
if (strcmp(*argv, flag) == 0) {
137-
return 1;
138-
}
139-
argv++;
140-
}
141-
return 0;
142-
}
143-
144132
/* takes an array containing the arguments that the user is allowed to enter on the command-line
145133
returns:
146134
- 1 if the user entered an invalid argument

src/bench_ecmult.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../include/secp256k1.h"
1010

1111
#include "util.h"
12+
#include "cli_util.h"
1213
#include "hash_impl.h"
1314
#include "field_impl.h"
1415
#include "group_impl.h"

src/bench_internal.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "assumptions.h"
1212
#include "util.h"
13+
#include "cli_util.h"
1314
#include "hash_impl.h"
1415
#include "field_impl.h"
1516
#include "group_impl.h"

src/cli_util.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/***********************************************************************
2+
* Copyright (c) 2023 Jonas Nick *
3+
* Distributed under the MIT software license, see the accompanying *
4+
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5+
***********************************************************************/
6+
7+
#ifndef SECP256K1_CLI_UTIL_H
8+
#define SECP256K1_CLI_UTIL_H
9+
10+
#include <string.h>
11+
12+
static int have_flag(int argc, char** argv, char *flag) {
13+
char** argm = argv + argc;
14+
argv++;
15+
while (argv != argm) {
16+
if (strcmp(*argv, flag) == 0) {
17+
return 1;
18+
}
19+
argv++;
20+
}
21+
return 0;
22+
}
23+
24+
#endif /* SECP256K1_CLI_UTIL_H */

src/tests.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "testrand_impl.h"
1717
#include "checkmem.h"
1818
#include "util.h"
19+
#include "cli_util.h"
1920

2021
#include "../contrib/lax_der_parsing.c"
2122
#include "../contrib/lax_der_privatekey_parsing.c"
@@ -28,7 +29,8 @@
2829

2930
#define CONDITIONAL_TEST(cnt, nam) if (COUNT < (cnt)) { printf("Skipping %s (iteration count too low)\n", nam); } else
3031

31-
static int COUNT = 64;
32+
#define DEFAULT_COUNT 64
33+
static int COUNT = DEFAULT_COUNT;
3234
static secp256k1_context *CTX = NULL;
3335
static secp256k1_context *STATIC_CTX = NULL;
3436

@@ -7428,6 +7430,21 @@ static void run_cmov_tests(void) {
74287430
ge_storage_cmov_test();
74297431
}
74307432

7433+
static void help(void) {
7434+
printf("The command ./tests runs a test suite on the code base.\n");
7435+
printf("\n");
7436+
printf("Some randomized tests are run for a certain number of iterations,\n");
7437+
printf("which is set to %d by default. This number can be altered by either\n", DEFAULT_COUNT);
7438+
printf("setting the environment variable SECP256K1_TEST_ITERS or by providing\n");
7439+
printf("the iteration count as a command line argument.\n");
7440+
printf("\n");
7441+
printf("Usage: ./tests [args]\n");
7442+
printf("Available arguments:\n");
7443+
printf(" help : display this help message and exit\n");
7444+
printf(" <count> : set the iteration count to <count>\n");
7445+
printf("\n");
7446+
}
7447+
74317448
int main(int argc, char **argv) {
74327449
/* Disable buffering for stdout to improve reliability of getting
74337450
* diagnostic information. Happens right at the start of main because
@@ -7437,6 +7454,15 @@ int main(int argc, char **argv) {
74377454
* unbuffered on all systems. */
74387455
setbuf(stderr, NULL);
74397456

7457+
if (argc > 1) {
7458+
if (have_flag(argc, argv, "-h")
7459+
|| have_flag(argc, argv, "--help")
7460+
|| have_flag(argc, argv, "help")) {
7461+
help();
7462+
return 0;
7463+
}
7464+
}
7465+
74407466
/* find iteration count */
74417467
if (argc > 1) {
74427468
COUNT = strtol(argv[1], NULL, 0);

0 commit comments

Comments
 (0)