tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestMozDbg.cpp (4012B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include <iostream>
      8 #include <type_traits>
      9 
     10 #include "gtest/gtest.h"
     11 #include "mozilla/DbgMacro.h"
     12 
     13 using namespace mozilla;
     14 
     15 #define TEST_MOZ_DBG_TYPE_IS(type_, expression_...)                    \
     16  static_assert(std::is_same_v<type_, decltype(MOZ_DBG(expression_))>, \
     17                "MOZ_DBG should return the indicated type")
     18 
     19 #define TEST_MOZ_DBG_TYPE_SAME(expression_...)                                 \
     20  static_assert(                                                               \
     21      std::is_same_v<decltype((expression_)), decltype(MOZ_DBG(expression_))>, \
     22      "MOZ_DBG should return the same type")
     23 
     24 struct Number {
     25  explicit Number(int aValue) : mValue(aValue) {}
     26 
     27  Number(const Number& aOther) = default;
     28 
     29  Number(Number&& aOther) : mValue(aOther.mValue) { aOther.mValue = 0; }
     30 
     31  Number& operator=(const Number& aOther) = default;
     32 
     33  Number& operator=(Number&& aOther) {
     34    mValue = aOther.mValue;
     35    aOther.mValue = 0;
     36    return *this;
     37  }
     38 
     39  ~Number() { mValue = -999; }
     40 
     41  int mValue;
     42 
     43  MOZ_DEFINE_DBG(Number, mValue)
     44 };
     45 
     46 struct MoveOnly {
     47  explicit MoveOnly(int aValue) : mValue(aValue) {}
     48 
     49  MoveOnly(const MoveOnly& aOther) = delete;
     50 
     51  MoveOnly(MoveOnly&& aOther) : mValue(aOther.mValue) { aOther.mValue = 0; }
     52 
     53  MoveOnly& operator=(MoveOnly& aOther) = default;
     54 
     55  MoveOnly& operator=(MoveOnly&& aOther) {
     56    mValue = aOther.mValue;
     57    aOther.mValue = 0;
     58    return *this;
     59  }
     60 
     61  int mValue;
     62 
     63  MOZ_DEFINE_DBG(MoveOnly)
     64 };
     65 
     66 void StaticAssertions() {
     67  int x = 123;
     68  Number y(123);
     69  Number z(234);
     70  MoveOnly w(456);
     71 
     72  // Static assertions.
     73 
     74  // lvalues
     75  TEST_MOZ_DBG_TYPE_SAME(x);        // int&
     76  TEST_MOZ_DBG_TYPE_SAME(y);        // Number&
     77  TEST_MOZ_DBG_TYPE_SAME(x = 234);  // int&
     78  TEST_MOZ_DBG_TYPE_SAME(y = z);    // Number&
     79  TEST_MOZ_DBG_TYPE_SAME(w);        // MoveOnly&
     80 
     81  // prvalues (which MOZ_DBG turns into xvalues by creating objects for them)
     82  TEST_MOZ_DBG_TYPE_IS(int&&, 123);
     83  TEST_MOZ_DBG_TYPE_IS(int&&, 1 + 2);
     84  TEST_MOZ_DBG_TYPE_IS(int*&&, &x);
     85  TEST_MOZ_DBG_TYPE_IS(int&&, x++);
     86  TEST_MOZ_DBG_TYPE_IS(Number&&, Number(123));
     87  TEST_MOZ_DBG_TYPE_IS(MoveOnly&&, MoveOnly(123));
     88 
     89  // xvalues
     90  TEST_MOZ_DBG_TYPE_SAME(std::move(y));  // int&&
     91  TEST_MOZ_DBG_TYPE_SAME(std::move(y));  // Number&&
     92  TEST_MOZ_DBG_TYPE_SAME(std::move(w));  // MoveOnly&
     93 
     94  (void)x;
     95  (void)y;
     96  (void)z;
     97 }
     98 
     99 TEST(MozDbg, ObjectValues)
    100 {
    101  // Test that moves and assignments all operate correctly with MOZ_DBG wrapped
    102  // around various parts of the expression.
    103 
    104  Number a(1);
    105  Number b(4);
    106 
    107  ASSERT_EQ(a.mValue, 1);
    108 
    109  MOZ_DBG(a.mValue);
    110  ASSERT_EQ(a.mValue, 1);
    111 
    112  MOZ_DBG(a.mValue + 1);
    113  ASSERT_EQ(a.mValue, 1);
    114 
    115  MOZ_DBG(a.mValue = 2);
    116  ASSERT_EQ(a.mValue, 2);
    117 
    118  MOZ_DBG(a).mValue = 3;
    119  ASSERT_EQ(a.mValue, 3);
    120 
    121  MOZ_DBG(a = b);
    122  ASSERT_EQ(a.mValue, 4);
    123  ASSERT_EQ(b.mValue, 4);
    124 
    125  b.mValue = 5;
    126  MOZ_DBG(a) = b;
    127  ASSERT_EQ(a.mValue, 5);
    128  ASSERT_EQ(b.mValue, 5);
    129 
    130  b.mValue = 6;
    131  MOZ_DBG(a = std::move(b));
    132  ASSERT_EQ(a.mValue, 6);
    133  ASSERT_EQ(b.mValue, 0);
    134 
    135  b.mValue = 7;
    136  MOZ_DBG(a) = std::move(b);
    137  ASSERT_EQ(a.mValue, 7);
    138  ASSERT_EQ(b.mValue, 0);
    139 
    140  b.mValue = 8;
    141  a = std::move(MOZ_DBG(b));
    142  ASSERT_EQ(a.mValue, 8);
    143  ASSERT_EQ(b.mValue, 0);
    144 
    145  a = MOZ_DBG(Number(9));
    146  ASSERT_EQ(a.mValue, 9);
    147 
    148  MoveOnly c(1);
    149  MoveOnly d(2);
    150 
    151  c = std::move(MOZ_DBG(d));
    152  ASSERT_EQ(c.mValue, 2);
    153  ASSERT_EQ(d.mValue, 0);
    154 
    155  c.mValue = 3;
    156  d.mValue = 4;
    157  c = MOZ_DBG(std::move(d));
    158  ASSERT_EQ(c.mValue, 4);
    159  ASSERT_EQ(d.mValue, 0);
    160 
    161  c.mValue = 5;
    162  d.mValue = 6;
    163  MOZ_DBG(c = std::move(d));
    164  ASSERT_EQ(c.mValue, 6);
    165  ASSERT_EQ(d.mValue, 0);
    166 
    167  c = MOZ_DBG(MoveOnly(7));
    168  ASSERT_EQ(c.mValue, 7);
    169 }