Line data Source code
1 44 : for(int i = 0; i < CYCLES; i++)
2 : {
3 : unsigned char hash_1[SHA512_DIGEST_LENGTH];
4 : unsigned char hash_2[SHA512_DIGEST_LENGTH];
5 40 : uint64_t random = 0;
6 :
7 40 : create(TYPE,test);
8 :
9 440 : for(int k = 0; k < CYCLES; k++)
10 : {
11 : // Generates a random number within a specified range (0-1000)
12 400 : ASSERT(SUCCESS == random_number_generator(&random,1,1000));
13 400 : size_t array_length = (size_t)random;
14 :
15 400 : size_t array_size = array_length * sizeof(TYPE);
16 400 : unsigned char *array = NULL;
17 400 : unsigned char *temp = (unsigned char *)realloc(array,array_size);
18 :
19 400 : ASSERT(temp != NULL);
20 :
21 400 : array = temp;
22 :
23 : // Fill array with random bytes
24 712069 : for(size_t j = 0; j < array_size; j++)
25 : {
26 711669 : ASSERT(SUCCESS == random_number_generator(&random,0,255));
27 711669 : array[j] = (unsigned char)random;
28 : }
29 :
30 : // Calculate SHA-512 hash
31 : SHA512_Context ctx;
32 400 : sha512_init(&ctx);
33 400 : sha512_update(&ctx,array,array_size);
34 400 : sha512_final(&ctx,hash_1);
35 :
36 : #if SHOW_TEST
37 : // Print array summary and hash
38 : echo(STDERR,"Test %d:%d array size: %zu bytes, array_length=%zu, sizeof(TYPE)=%zu bytes\n",i,k,array_size,array_length,sizeof(TYPE));
39 : echo(STDERR,"Test %d:%d SHA-512 hash: ",i,k);
40 : print_hash(hash_1);
41 : #endif
42 :
43 : // Create a TYPE memory with randomly real reallocation or not
44 400 : ASSERT(SUCCESS == random_number_generator(&random,0,1));
45 400 : bool true_reduce = (bool)random;
46 : (void)true_reduce; /* resizing always keeps spare capacity */
47 400 : ASSERT(SUCCESS == resize(test,array_length));
48 :
49 400 : TYPE *test_data = data(TYPE,test);
50 400 : ASSERT(test_data != NULL);
51 :
52 400 : if(test_data != NULL)
53 : {
54 400 : memcpy(test_data,array,test->length * sizeof(TYPE));
55 : }
56 :
57 : // Calculate SHA-512 hash
58 400 : sha512_init(&ctx);
59 400 : const TYPE *test_view = cdata(TYPE,test);
60 400 : ASSERT(test_view != NULL);
61 :
62 400 : if(test_view != NULL)
63 : {
64 400 : sha512_update(&ctx,(const unsigned char *)test_view,test->length * sizeof(TYPE));
65 : }
66 400 : sha512_final(&ctx,hash_2);
67 :
68 : #if SHOW_TEST
69 : // Print array summary and hash
70 : echo(STDERR,"Test %d:%d array size: %zu bytes\n",i,k,test->length * sizeof(TYPE));
71 : echo(STDERR,"Test %d:%d SHA-512 hash: ",i,k);
72 : print_hash(hash_2);
73 : #endif
74 :
75 400 : ASSERT(0 == memcmp(hash_1,hash_2,(size_t)SHA512_DIGEST_LENGTH));
76 :
77 400 : reset(&array);
78 : }
79 :
80 : // free an empty TYPE array
81 40 : del(test);
82 : }
|