nsprpub/pr/tests/atomic.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "prio.h"
michael@0 7 #include "prprf.h"
michael@0 8 #include "pratom.h"
michael@0 9
michael@0 10 /*
michael@0 11 * TODO: create a macro to generate the six lines of code that are repeated
michael@0 12 * for every test. Also rewrite the statement
michael@0 13 * result = result | ((EXPRESSION) ? 0 : 1);
michael@0 14 * as
michael@0 15 * result |= !(EXPRESSION);
michael@0 16 */
michael@0 17
michael@0 18 int main(int argc, char **argv)
michael@0 19 {
michael@0 20 PRInt32 rv, oldval, test, result = 0;
michael@0 21 PRFileDesc *output = PR_GetSpecialFD(PR_StandardOutput);
michael@0 22
michael@0 23 /***********************/
michael@0 24 /* Test the functions. */
michael@0 25 /***********************/
michael@0 26
michael@0 27 oldval = test = -2;
michael@0 28 rv = PR_AtomicIncrement(&test);
michael@0 29 result = result | ((rv == -1) ? 0 : 1);
michael@0 30 PR_fprintf(
michael@0 31 output, "PR_AtomicIncrement(%d) == %d: %s\n",
michael@0 32 oldval, rv, (rv == -1) ? "PASSED" : "FAILED");
michael@0 33 oldval = test;
michael@0 34 rv = PR_AtomicIncrement(&test);
michael@0 35 result = result | ((rv == 0) ? 0 : 1);
michael@0 36 PR_fprintf(
michael@0 37 output, "PR_AtomicIncrement(%d) == %d: %s\n",
michael@0 38 oldval, rv, (rv == 0) ? "PASSED" : "FAILED");
michael@0 39 oldval = test;
michael@0 40 rv = PR_AtomicIncrement(&test);
michael@0 41 result = result | ((rv == 1) ? 0 : 1);
michael@0 42 PR_fprintf(
michael@0 43 output, "PR_AtomicIncrement(%d) == %d: %s\n",
michael@0 44 oldval, rv, (rv == 1) ? "PASSED" : "FAILED");
michael@0 45
michael@0 46 oldval = test = -2;
michael@0 47 rv = PR_AtomicAdd(&test,1);
michael@0 48 result = result | ((rv == -1) ? 0 : 1);
michael@0 49 PR_fprintf(
michael@0 50 output, "PR_AtomicAdd(%d,%d) == %d: %s\n",
michael@0 51 oldval, 1, rv, (rv == -1) ? "PASSED" : "FAILED");
michael@0 52 oldval = test;
michael@0 53 rv = PR_AtomicAdd(&test, 4);
michael@0 54 result = result | ((rv == 3) ? 0 : 1);
michael@0 55 PR_fprintf(
michael@0 56 output, "PR_AtomicAdd(%d,%d) == %d: %s\n",
michael@0 57 oldval, 4, rv, (rv == 3) ? "PASSED" : "FAILED");
michael@0 58 oldval = test;
michael@0 59 rv = PR_AtomicAdd(&test, -6);
michael@0 60 result = result | ((rv == -3) ? 0 : 1);
michael@0 61 PR_fprintf(
michael@0 62 output, "PR_AtomicAdd(%d,%d) == %d: %s\n",
michael@0 63 oldval, -6, rv, (rv == -3) ? "PASSED" : "FAILED");
michael@0 64
michael@0 65 oldval = test = 2;
michael@0 66 rv = PR_AtomicDecrement(&test);
michael@0 67 result = result | ((rv == 1) ? 0 : 1);
michael@0 68 PR_fprintf(
michael@0 69 output, "PR_AtomicDecrement(%d) == %d: %s\n",
michael@0 70 oldval, rv, (rv == 1) ? "PASSED" : "FAILED");
michael@0 71 oldval = test;
michael@0 72 rv = PR_AtomicDecrement(&test);
michael@0 73 result = result | ((rv == 0) ? 0 : 1);
michael@0 74 PR_fprintf(
michael@0 75 output, "PR_AtomicDecrement(%d) == %d: %s\n",
michael@0 76 oldval, rv, (rv == 0) ? "PASSED" : "FAILED");
michael@0 77 oldval = test;
michael@0 78 rv = PR_AtomicDecrement(&test);
michael@0 79 result = result | ((rv == -1) ? 0 : 1);
michael@0 80 PR_fprintf(
michael@0 81 output, "PR_AtomicDecrement(%d) == %d: %s\n",
michael@0 82 oldval, rv, (rv == -1) ? "PASSED" : "FAILED");
michael@0 83
michael@0 84 /* set to a different value */
michael@0 85 oldval = test = -2;
michael@0 86 rv = PR_AtomicSet(&test, 2);
michael@0 87 result = result | (((rv == -2) && (test == 2)) ? 0 : 1);
michael@0 88 PR_fprintf(
michael@0 89 output, "PR_AtomicSet(%d, %d) == %d: %s\n",
michael@0 90 oldval, 2, rv, ((rv == -2) && (test == 2)) ? "PASSED" : "FAILED");
michael@0 91
michael@0 92 /* set to the same value */
michael@0 93 oldval = test = -2;
michael@0 94 rv = PR_AtomicSet(&test, -2);
michael@0 95 result = result | (((rv == -2) && (test == -2)) ? 0 : 1);
michael@0 96 PR_fprintf(
michael@0 97 output, "PR_AtomicSet(%d, %d) == %d: %s\n",
michael@0 98 oldval, -2, rv, ((rv == -2) && (test == -2)) ? "PASSED" : "FAILED");
michael@0 99
michael@0 100 /***********************/
michael@0 101 /* Test the macros. */
michael@0 102 /***********************/
michael@0 103
michael@0 104 oldval = test = -2;
michael@0 105 rv = PR_ATOMIC_INCREMENT(&test);
michael@0 106 result = result | ((rv == -1) ? 0 : 1);
michael@0 107 PR_fprintf(
michael@0 108 output, "PR_ATOMIC_INCREMENT(%d) == %d: %s\n",
michael@0 109 oldval, rv, (rv == -1) ? "PASSED" : "FAILED");
michael@0 110 oldval = test;
michael@0 111 rv = PR_ATOMIC_INCREMENT(&test);
michael@0 112 result = result | ((rv == 0) ? 0 : 1);
michael@0 113 PR_fprintf(
michael@0 114 output, "PR_ATOMIC_INCREMENT(%d) == %d: %s\n",
michael@0 115 oldval, rv, (rv == 0) ? "PASSED" : "FAILED");
michael@0 116 oldval = test;
michael@0 117 rv = PR_ATOMIC_INCREMENT(&test);
michael@0 118 result = result | ((rv == 1) ? 0 : 1);
michael@0 119 PR_fprintf(
michael@0 120 output, "PR_ATOMIC_INCREMENT(%d) == %d: %s\n",
michael@0 121 oldval, rv, (rv == 1) ? "PASSED" : "FAILED");
michael@0 122
michael@0 123 oldval = test = -2;
michael@0 124 rv = PR_ATOMIC_ADD(&test,1);
michael@0 125 result = result | ((rv == -1) ? 0 : 1);
michael@0 126 PR_fprintf(
michael@0 127 output, "PR_ATOMIC_ADD(%d,%d) == %d: %s\n",
michael@0 128 oldval, 1, rv, (rv == -1) ? "PASSED" : "FAILED");
michael@0 129 oldval = test;
michael@0 130 rv = PR_ATOMIC_ADD(&test, 4);
michael@0 131 result = result | ((rv == 3) ? 0 : 1);
michael@0 132 PR_fprintf(
michael@0 133 output, "PR_ATOMIC_ADD(%d,%d) == %d: %s\n",
michael@0 134 oldval, 4, rv, (rv == 3) ? "PASSED" : "FAILED");
michael@0 135 oldval = test;
michael@0 136 rv = PR_ATOMIC_ADD(&test, -6);
michael@0 137 result = result | ((rv == -3) ? 0 : 1);
michael@0 138 PR_fprintf(
michael@0 139 output, "PR_ATOMIC_ADD(%d,%d) == %d: %s\n",
michael@0 140 oldval, -6, rv, (rv == -3) ? "PASSED" : "FAILED");
michael@0 141
michael@0 142 oldval = test = 2;
michael@0 143 rv = PR_ATOMIC_DECREMENT(&test);
michael@0 144 result = result | ((rv == 1) ? 0 : 1);
michael@0 145 PR_fprintf(
michael@0 146 output, "PR_ATOMIC_DECREMENT(%d) == %d: %s\n",
michael@0 147 oldval, rv, (rv == 1) ? "PASSED" : "FAILED");
michael@0 148 oldval = test;
michael@0 149 rv = PR_ATOMIC_DECREMENT(&test);
michael@0 150 result = result | ((rv == 0) ? 0 : 1);
michael@0 151 PR_fprintf(
michael@0 152 output, "PR_ATOMIC_DECREMENT(%d) == %d: %s\n",
michael@0 153 oldval, rv, (rv == 0) ? "PASSED" : "FAILED");
michael@0 154 oldval = test;
michael@0 155 rv = PR_ATOMIC_DECREMENT(&test);
michael@0 156 result = result | ((rv == -1) ? 0 : 1);
michael@0 157 PR_fprintf(
michael@0 158 output, "PR_ATOMIC_DECREMENT(%d) == %d: %s\n",
michael@0 159 oldval, rv, (rv == -1) ? "PASSED" : "FAILED");
michael@0 160
michael@0 161 /* set to a different value */
michael@0 162 oldval = test = -2;
michael@0 163 rv = PR_ATOMIC_SET(&test, 2);
michael@0 164 result = result | (((rv == -2) && (test == 2)) ? 0 : 1);
michael@0 165 PR_fprintf(
michael@0 166 output, "PR_ATOMIC_SET(%d, %d) == %d: %s\n",
michael@0 167 oldval, 2, rv, ((rv == -2) && (test == 2)) ? "PASSED" : "FAILED");
michael@0 168
michael@0 169 /* set to the same value */
michael@0 170 oldval = test = -2;
michael@0 171 rv = PR_ATOMIC_SET(&test, -2);
michael@0 172 result = result | (((rv == -2) && (test == -2)) ? 0 : 1);
michael@0 173 PR_fprintf(
michael@0 174 output, "PR_ATOMIC_SET(%d, %d) == %d: %s\n",
michael@0 175 oldval, -2, rv, ((rv == -2) && (test == -2)) ? "PASSED" : "FAILED");
michael@0 176
michael@0 177 PR_fprintf(
michael@0 178 output, "Atomic operations test %s\n",
michael@0 179 (result == 0) ? "PASSED" : "FAILED");
michael@0 180 return result;
michael@0 181 } /* main */
michael@0 182
michael@0 183 /* atomic.c */

mercurial