Skip to content

Commit

Permalink
Merge pull request #158 from ajblane/thread-safe-test
Browse files Browse the repository at this point in the history
test: Add thread-safe test
  • Loading branch information
marktwtn committed May 28, 2019
2 parents 988198f + 476d17a commit f2c5292
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ TESTS = \
trinary \
curl \
dcurl \
pow
pow \
multi-pow

TESTS := $(addprefix $(OUT)/test-, $(TESTS))

Expand Down
108 changes: 108 additions & 0 deletions tests/test-multi-pow.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/* Test program for thread-safe dcurl */
#include <pthread.h>
#include "common.h"
#include "dcurl.h"

#define THREAD_MAX 10

typedef struct _dcurl_item dcurl_item;
struct _dcurl_item {
int mwm;
int8_t input_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */
int8_t output_trytes[TRANSACTION_TRYTES_LENGTH]; /* 2673 */
};

void *dcurl_entry_cb(void *arg)
{
dcurl_item *item = (dcurl_item *) arg;
/* test dcurl Implementation with mwm = 14 */
int8_t *ret_trytes =
dcurl_entry(item->input_trytes, item->mwm, 0);
assert(ret_trytes && "dcurl_entry() failed");
memcpy(item->output_trytes, ret_trytes, TRANSACTION_TRYTES_LENGTH);
free(ret_trytes);

pthread_exit(NULL);
}

int main()
{
char *trytes =
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"99999999999999999A9RGRKVGWMWMKOLVMDFWJUHNUNYWZTJADGGPZGXNLERLXYWJE9WQH"
"WWBMCPZMVVMJUMWWBLZLNMLDCGDJ999999999999999999999999999999999999999999"
"999999999999YGYQIVD99999999999999999999TXEFLKNPJRBYZPORHZU9CEMFIFVVQBU"
"STDGSJCZMBTZCDTTJVUFPTCCVHHORPMGCURKTH9VGJIXUQJVHK99999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999999999999999999999999999999999999999999999999999999999999"
"9999999999999";

int mwm = 14;
pthread_t threads[THREAD_MAX];
dcurl_item items[THREAD_MAX];

dcurl_init();

for (int i = 0; i < THREAD_MAX; i++) {
memcpy(items[i].input_trytes, trytes, TRANSACTION_TRYTES_LENGTH);
items[i].mwm = mwm;
pthread_create(&threads[i], NULL, dcurl_entry_cb,
(void *) &items[i]);
}

for (int i = 0; i < THREAD_MAX; i++)
pthread_join(threads[i], NULL);

for (int i = 0; i < THREAD_MAX; i++) {
Trytes_t *trytes_t =
initTrytes(items[i].output_trytes, TRANSACTION_TRYTES_LENGTH);
assert(trytes_t && "initTrytes() failed");
Trytes_t *hash_trytes = hashTrytes(trytes_t);
assert(hash_trytes && "hashTrytes() failed");

/* Validation */
Trits_t *ret_trits = trits_from_trytes(hash_trytes);
for (int j = 243 - 1; j >= 243 - items[i].mwm; j--) {
assert(ret_trits->data[j] == 0 && "Validation failed");
}

freeTrobject(trytes_t);
freeTrobject(hash_trytes);
freeTrobject(ret_trits);
}

dcurl_destroy();

return 0;
}

0 comments on commit f2c5292

Please sign in to comment.