This repository has been archived by the owner on Dec 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test): Add test for tests for core funcs
Functions that are not covered by `api/driver.c` are covered by `driver_core.c`. And those common functions are moved from `driver.c` to `common.h` and `common.c`.
- Loading branch information
Showing
6 changed files
with
298 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include "common.h" | ||
#include "accelerator/cli_info.h" | ||
#include "stdlib.h" | ||
|
||
static struct ta_cli_argument_s driver_cli_arguments_g[] = { | ||
{"hash", required_argument, NULL, 'H', "testing transaction hashes"}, | ||
{"tag", required_argument, NULL, 'T', "testing transaction tag"}, | ||
{NULL, 0, NULL, 0, NULL}}; | ||
|
||
struct option* driver_cli_build_options() { | ||
int driver_cli_arg_size = sizeof(driver_cli_arguments_g) / sizeof(driver_cli_arguments_g[0]); | ||
struct option* driver_long_options = | ||
(struct option*)realloc(cli_build_options(), (cli_cmd_num + 2) * sizeof(struct option)); | ||
for (int i = 0; i < driver_cli_arg_size; i++) { | ||
driver_long_options[(cli_cmd_num - 1) + i].name = driver_cli_arguments_g[i].name; | ||
driver_long_options[(cli_cmd_num - 1) + i].has_arg = driver_cli_arguments_g[i].has_arg; | ||
driver_long_options[(cli_cmd_num - 1) + i].flag = driver_cli_arguments_g[i].flag; | ||
driver_long_options[(cli_cmd_num - 1) + i].val = driver_cli_arguments_g[i].val; | ||
} | ||
|
||
return driver_long_options; | ||
} | ||
|
||
status_t driver_core_cli_init(ta_core_t* const core, int argc, char** argv, driver_test_cases_t* test_cases) { | ||
int key = 0; | ||
status_t ret = SC_OK; | ||
struct option* long_options = driver_cli_build_options(); | ||
|
||
while ((key = getopt_long(argc, argv, "hvH:T:", long_options, NULL)) != -1) { | ||
switch (key) { | ||
case 'h': | ||
ta_usage(); | ||
exit(EXIT_SUCCESS); | ||
case 'v': | ||
printf("%s\n", TA_VERSION); | ||
exit(EXIT_SUCCESS); | ||
case 'H': | ||
// Take the arguments as testing transaction hashes | ||
for (int i = 0; i < TXN_HASH_NUM; i++) { | ||
if (!test_cases->txn_hash[i]) { | ||
test_cases->txn_hash[i] = optarg; | ||
} | ||
} | ||
break; | ||
case 'T': | ||
// Take the arguments as testing transaction tag | ||
test_cases->tag = optarg; | ||
break; | ||
default: | ||
ret = cli_core_set(core, key, optarg); | ||
break; | ||
} | ||
if (ret != SC_OK) { | ||
break; | ||
} | ||
} | ||
|
||
free(long_options); | ||
return ret; | ||
} | ||
|
||
static double diff_time(struct timespec start, struct timespec end) { | ||
struct timespec diff; | ||
if (end.tv_nsec - start.tv_nsec < 0) { | ||
diff.tv_sec = end.tv_sec - start.tv_sec - 1; | ||
diff.tv_nsec = end.tv_nsec - start.tv_nsec + 1000000000; | ||
} else { | ||
diff.tv_sec = end.tv_sec - start.tv_sec; | ||
diff.tv_nsec = end.tv_nsec - start.tv_nsec; | ||
} | ||
return (diff.tv_sec + diff.tv_nsec / 1000000000.0); | ||
} | ||
|
||
void test_time_end(struct timespec* start, struct timespec* end, double* sum) { | ||
clock_gettime(CLOCK_REALTIME, end); | ||
double difference = diff_time(*start, *end); | ||
#if defined(ENABLE_STAT) | ||
printf("%lf\n", difference); | ||
#endif | ||
*sum += difference; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (C) 2020 BiiLabs Co., Ltd. and Contributors | ||
* All Rights Reserved. | ||
* This is free software; you can redistribute it and/or modify it under the | ||
* terms of the MIT license. A copy of the license can be found in the file | ||
* "LICENSE" at the root of this distribution. | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <time.h> | ||
#include "accelerator/config.h" | ||
#include "common/ta_errors.h" | ||
|
||
#define TXN_HASH_NUM 2 | ||
|
||
typedef struct driver_test_cases_s { | ||
char* txn_hash[TXN_HASH_NUM]; | ||
char* tag; | ||
} driver_test_cases_t; | ||
|
||
/** | ||
* Set option list for `get_long()` | ||
* | ||
* @return | ||
* - pointer of a `struct option` array on success | ||
* - NULL on error | ||
*/ | ||
struct option* driver_cli_build_options(); | ||
|
||
/** | ||
* Initializes configurations with default values for driver tests | ||
* | ||
* @param core[in] Pointer to Tangle-accelerator core configuration structure | ||
* @param argc[in] Pointer to Tangle-accelerator core configuration structure | ||
* @param argv[in] Pointer to Tangle-accelerator core configuration structure | ||
* @param test_cases[in] Pointer to Tangle-accelerator core configuration structure | ||
* | ||
* @return | ||
* - SC_OK on success | ||
* - non-zero on error | ||
*/ | ||
status_t driver_core_cli_init(ta_core_t* const core, int argc, char** argv, driver_test_cases_t* test_cases); | ||
|
||
/** | ||
* Start testing clock | ||
* | ||
* @param start[out] `struct timespec` object for recording starting time. | ||
* | ||
*/ | ||
static inline void test_time_start(struct timespec* start) { clock_gettime(CLOCK_REALTIME, start); } | ||
|
||
/** | ||
* Initializes configurations with default values | ||
* | ||
* @param start[in] `struct timespec` object for recording starting time. | ||
* @param end[out] `struct timespec` object for recording ending time. | ||
* @param start[out] The total elapsing time. | ||
* | ||
*/ | ||
void test_time_end(struct timespec* start, struct timespec* end, double* sum); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.