]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/zstd/tests/pool.c
MFV r322229: 7600 zfs rollback should pass target snapshot to kernel
[FreeBSD/FreeBSD.git] / contrib / zstd / tests / pool.c
1 #include "pool.h"
2 #include "threading.h"
3 #include <stddef.h>
4 #include <stdio.h>
5
6 #define ASSERT_TRUE(p)                                                         \
7   do {                                                                         \
8     if (!(p)) {                                                                \
9       return 1;                                                                \
10     }                                                                          \
11   } while (0)
12 #define ASSERT_FALSE(p) ASSERT_TRUE(!(p))
13 #define ASSERT_EQ(lhs, rhs) ASSERT_TRUE((lhs) == (rhs))
14
15 struct data {
16   pthread_mutex_t mutex;
17   unsigned data[16];
18   size_t i;
19 };
20
21 void fn(void *opaque) {
22   struct data *data = (struct data *)opaque;
23   pthread_mutex_lock(&data->mutex);
24   data->data[data->i] = data->i;
25   ++data->i;
26   pthread_mutex_unlock(&data->mutex);
27 }
28
29 int testOrder(size_t numThreads, size_t queueSize) {
30   struct data data;
31   POOL_ctx *ctx = POOL_create(numThreads, queueSize);
32   ASSERT_TRUE(ctx);
33   data.i = 0;
34   pthread_mutex_init(&data.mutex, NULL);
35   {
36     size_t i;
37     for (i = 0; i < 16; ++i) {
38       POOL_add(ctx, &fn, &data);
39     }
40   }
41   POOL_free(ctx);
42   ASSERT_EQ(16, data.i);
43   {
44     size_t i;
45     for (i = 0; i < data.i; ++i) {
46       ASSERT_EQ(i, data.data[i]);
47     }
48   }
49   pthread_mutex_destroy(&data.mutex);
50   return 0;
51 }
52
53 int main(int argc, const char **argv) {
54   size_t numThreads;
55   for (numThreads = 1; numThreads <= 4; ++numThreads) {
56     size_t queueSize;
57     for (queueSize = 1; queueSize <= 2; ++queueSize) {
58       if (testOrder(numThreads, queueSize)) {
59         printf("FAIL: testOrder\n");
60         return 1;
61       }
62     }
63   }
64   printf("PASS: testOrder\n");
65   (void)argc;
66   (void)argv;
67   return (POOL_create(0, 1) || POOL_create(1, 0)) ? printf("FAIL: testInvalid\n"), 1
68                                                   : printf("PASS: testInvalid\n"), 0;
69   return 0;
70 }