]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/zstd/tests/poolTests.c
Import zstandard 1.3.1
[FreeBSD/FreeBSD.git] / contrib / zstd / tests / poolTests.c
1 /*
2  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under both the BSD-style license (found in the
6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7  * in the COPYING file in the root directory of this source tree).
8  */
9
10
11 #include "pool.h"
12 #include "threading.h"
13 #include "util.h"
14 #include <stddef.h>
15 #include <stdio.h>
16
17 #define ASSERT_TRUE(p)                                                         \
18   do {                                                                         \
19     if (!(p)) {                                                                \
20       return 1;                                                                \
21     }                                                                          \
22   } while (0)
23 #define ASSERT_FALSE(p) ASSERT_TRUE(!(p))
24 #define ASSERT_EQ(lhs, rhs) ASSERT_TRUE((lhs) == (rhs))
25
26 struct data {
27   pthread_mutex_t mutex;
28   unsigned data[16];
29   size_t i;
30 };
31
32 void fn(void *opaque) {
33   struct data *data = (struct data *)opaque;
34   pthread_mutex_lock(&data->mutex);
35   data->data[data->i] = data->i;
36   ++data->i;
37   pthread_mutex_unlock(&data->mutex);
38 }
39
40 int testOrder(size_t numThreads, size_t queueSize) {
41   struct data data;
42   POOL_ctx *ctx = POOL_create(numThreads, queueSize);
43   ASSERT_TRUE(ctx);
44   data.i = 0;
45   pthread_mutex_init(&data.mutex, NULL);
46   {
47     size_t i;
48     for (i = 0; i < 16; ++i) {
49       POOL_add(ctx, &fn, &data);
50     }
51   }
52   POOL_free(ctx);
53   ASSERT_EQ(16, data.i);
54   {
55     size_t i;
56     for (i = 0; i < data.i; ++i) {
57       ASSERT_EQ(i, data.data[i]);
58     }
59   }
60   pthread_mutex_destroy(&data.mutex);
61   return 0;
62 }
63
64 void waitFn(void *opaque) {
65   (void)opaque;
66   UTIL_sleepMilli(1);
67 }
68
69 /* Tests for deadlock */
70 int testWait(size_t numThreads, size_t queueSize) {
71   struct data data;
72   POOL_ctx *ctx = POOL_create(numThreads, queueSize);
73   ASSERT_TRUE(ctx);
74   {
75     size_t i;
76     for (i = 0; i < 16; ++i) {
77         POOL_add(ctx, &waitFn, &data);
78     }
79   }
80   POOL_free(ctx);
81   return 0;
82 }
83
84 int main(int argc, const char **argv) {
85   size_t numThreads;
86   for (numThreads = 1; numThreads <= 4; ++numThreads) {
87     size_t queueSize;
88     for (queueSize = 0; queueSize <= 2; ++queueSize) {
89       if (testOrder(numThreads, queueSize)) {
90         printf("FAIL: testOrder\n");
91         return 1;
92       }
93       if (testWait(numThreads, queueSize)) {
94         printf("FAIL: testWait\n");
95         return 1;
96       }
97     }
98   }
99   printf("PASS: testOrder\n");
100   (void)argc;
101   (void)argv;
102   return (POOL_create(0, 1)) ? printf("FAIL: testInvalid\n"), 1
103                              : printf("PASS: testInvalid\n"), 0;
104   return 0;
105 }