]> granicus.if.org Git - postgresql/commitdiff
Test pg_atomic_fetch_add_ with variable addend and 16-bit edge cases.
authorNoah Misch <noah@leadboat.com>
Sat, 14 Sep 2019 02:33:30 +0000 (19:33 -0700)
committerNoah Misch <noah@leadboat.com>
Sat, 14 Sep 2019 02:33:46 +0000 (19:33 -0700)
Back-patch to 9.5, which introduced these functions.

Reviewed by Tom Lane.

Discussion: https://postgr.es/m/20190831071157.GA3251746@rfd.leadboat.com

src/test/regress/regress.c

index 7f03b7e857bfbbf852bf57761b430c135bcabb0b..b00b76f81ef3c831986e1108bdaf5e4711dbc582 100644 (file)
@@ -687,7 +687,7 @@ test_atomic_uint32(void)
        if (pg_atomic_read_u32(&var) != 3)
                elog(ERROR, "atomic_read_u32() #2 wrong");
 
-       if (pg_atomic_fetch_add_u32(&var, 1) != 3)
+       if (pg_atomic_fetch_add_u32(&var, pg_atomic_read_u32(&var) - 2) != 3)
                elog(ERROR, "atomic_fetch_add_u32() #1 wrong");
 
        if (pg_atomic_fetch_sub_u32(&var, 1) != 4)
@@ -712,6 +712,20 @@ test_atomic_uint32(void)
        if (pg_atomic_fetch_add_u32(&var, INT_MAX) != INT_MAX)
                elog(ERROR, "pg_atomic_add_fetch_u32() #3 wrong");
 
+       pg_atomic_fetch_add_u32(&var, 2);       /* wrap to 0 */
+
+       if (pg_atomic_fetch_add_u32(&var, PG_INT16_MAX) != 0)
+               elog(ERROR, "pg_atomic_fetch_add_u32() #3 wrong");
+
+       if (pg_atomic_fetch_add_u32(&var, PG_INT16_MAX + 1) != PG_INT16_MAX)
+               elog(ERROR, "pg_atomic_fetch_add_u32() #4 wrong");
+
+       if (pg_atomic_fetch_add_u32(&var, PG_INT16_MIN) != 2 * PG_INT16_MAX + 1)
+               elog(ERROR, "pg_atomic_fetch_add_u32() #5 wrong");
+
+       if (pg_atomic_fetch_add_u32(&var, PG_INT16_MIN - 1) != PG_INT16_MAX)
+               elog(ERROR, "pg_atomic_fetch_add_u32() #6 wrong");
+
        pg_atomic_fetch_add_u32(&var, 1);       /* top up to UINT_MAX */
 
        if (pg_atomic_read_u32(&var) != UINT_MAX)
@@ -787,7 +801,7 @@ test_atomic_uint64(void)
        if (pg_atomic_read_u64(&var) != 3)
                elog(ERROR, "atomic_read_u64() #2 wrong");
 
-       if (pg_atomic_fetch_add_u64(&var, 1) != 3)
+       if (pg_atomic_fetch_add_u64(&var, pg_atomic_read_u64(&var) - 2) != 3)
                elog(ERROR, "atomic_fetch_add_u64() #1 wrong");
 
        if (pg_atomic_fetch_sub_u64(&var, 1) != 4)