| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-require-atomic-builtins "" } |
| 3 | |
| 4 | // std::abs from <cstdlib> is not freestanding. |
| 5 | // { dg-require-effective-target hosted } |
| 6 | |
| 7 | // Copyright (C) 2012-2025 Free Software Foundation, Inc. |
| 8 | // |
| 9 | // This file is part of the GNU ISO C++ Library. This library is free |
| 10 | // software; you can redistribute it and/or modify it under the |
| 11 | // terms of the GNU General Public License as published by the |
| 12 | // Free Software Foundation; either version 3, or (at your option) |
| 13 | // any later version. |
| 14 | |
| 15 | // This library is distributed in the hope that it will be useful, |
| 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | // GNU General Public License for more details. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License along |
| 21 | // with this library; see the file COPYING3. If not see |
| 22 | // <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | #include <atomic> |
| 25 | #include <cstdlib> //std::abs |
| 26 | #include <testsuite_hooks.h> |
| 27 | |
| 28 | // libstdc++/51811 |
| 29 | // pointer arithimetic vs. atomic<_Tp*> specialization |
| 30 | int main(void) |
| 31 | { |
| 32 | using namespace std; |
| 33 | |
| 34 | typedef int value_type; |
| 35 | const size_t n = 2; |
| 36 | value_type value = 42; |
| 37 | atomic<value_type*> p, p2, p3; |
| 38 | |
| 39 | // operator++ |
| 40 | { |
| 41 | p = &value; |
| 42 | p2 = p++; |
| 43 | VERIFY (p != p2); |
| 44 | |
| 45 | value_type* vp(p); |
| 46 | value_type* vp2(p2); |
| 47 | ptrdiff_t dist = reinterpret_cast<char*>(vp) - reinterpret_cast<char*>(vp2); |
| 48 | VERIFY ( std::abs(dist) == sizeof(value_type)); |
| 49 | |
| 50 | p = &value; |
| 51 | p3 = ++p; |
| 52 | VERIFY (p == p3); |
| 53 | } |
| 54 | |
| 55 | // operator-- |
| 56 | { |
| 57 | p = &value; |
| 58 | p2 = p--; |
| 59 | VERIFY (p != p2); |
| 60 | |
| 61 | value_type* vp(p); |
| 62 | value_type* vp2(p2); |
| 63 | ptrdiff_t dist = reinterpret_cast<char*>(vp) - reinterpret_cast<char*>(vp2); |
| 64 | VERIFY ( std::abs(dist) == sizeof(value_type)); |
| 65 | |
| 66 | p = &value; |
| 67 | p3 = --p; |
| 68 | VERIFY (p == p3); |
| 69 | } |
| 70 | |
| 71 | // operator+= |
| 72 | { |
| 73 | p = &value; |
| 74 | value_type* vp(p); |
| 75 | p+=n; |
| 76 | value_type* vp2(p); |
| 77 | ptrdiff_t dist = reinterpret_cast<char*>(vp) - reinterpret_cast<char*>(vp2); |
| 78 | VERIFY ( std::abs(dist) == sizeof(value_type) * n); |
| 79 | } |
| 80 | |
| 81 | // operator-= |
| 82 | { |
| 83 | p = &value; |
| 84 | value_type* vp(p); |
| 85 | p-=n; |
| 86 | value_type* vp2(p); |
| 87 | ptrdiff_t dist = reinterpret_cast<char*>(vp) - reinterpret_cast<char*>(vp2); |
| 88 | VERIFY ( std::abs(dist) == sizeof(value_type) * n); |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |