compare_test.cc
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/types/compare.h"
00016 
00017 #include "gtest/gtest.h"
00018 #include "absl/base/casts.h"
00019 
00020 namespace absl {
00021 namespace {
00022 
00023 // This is necessary to avoid a bunch of lint warnings suggesting that we use
00024 // EXPECT_EQ/etc., which doesn't work in this case because they convert the `0`
00025 // to an int, which can't be converted to the unspecified zero type.
00026 bool Identity(bool b) { return b; }
00027 
00028 TEST(Compare, WeakEquality) {
00029   EXPECT_TRUE(Identity(weak_equality::equivalent == 0));
00030   EXPECT_TRUE(Identity(0 == weak_equality::equivalent));
00031   EXPECT_TRUE(Identity(weak_equality::nonequivalent != 0));
00032   EXPECT_TRUE(Identity(0 != weak_equality::nonequivalent));
00033 }
00034 
00035 TEST(Compare, StrongEquality) {
00036   EXPECT_TRUE(Identity(strong_equality::equal == 0));
00037   EXPECT_TRUE(Identity(0 == strong_equality::equal));
00038   EXPECT_TRUE(Identity(strong_equality::nonequal != 0));
00039   EXPECT_TRUE(Identity(0 != strong_equality::nonequal));
00040   EXPECT_TRUE(Identity(strong_equality::equivalent == 0));
00041   EXPECT_TRUE(Identity(0 == strong_equality::equivalent));
00042   EXPECT_TRUE(Identity(strong_equality::nonequivalent != 0));
00043   EXPECT_TRUE(Identity(0 != strong_equality::nonequivalent));
00044 }
00045 
00046 TEST(Compare, PartialOrdering) {
00047   EXPECT_TRUE(Identity(partial_ordering::less < 0));
00048   EXPECT_TRUE(Identity(0 > partial_ordering::less));
00049   EXPECT_TRUE(Identity(partial_ordering::less <= 0));
00050   EXPECT_TRUE(Identity(0 >= partial_ordering::less));
00051   EXPECT_TRUE(Identity(partial_ordering::equivalent == 0));
00052   EXPECT_TRUE(Identity(0 == partial_ordering::equivalent));
00053   EXPECT_TRUE(Identity(partial_ordering::greater > 0));
00054   EXPECT_TRUE(Identity(0 < partial_ordering::greater));
00055   EXPECT_TRUE(Identity(partial_ordering::greater >= 0));
00056   EXPECT_TRUE(Identity(0 <= partial_ordering::greater));
00057   EXPECT_TRUE(Identity(partial_ordering::unordered != 0));
00058   EXPECT_TRUE(Identity(0 != partial_ordering::unordered));
00059   EXPECT_FALSE(Identity(partial_ordering::unordered < 0));
00060   EXPECT_FALSE(Identity(0 < partial_ordering::unordered));
00061   EXPECT_FALSE(Identity(partial_ordering::unordered <= 0));
00062   EXPECT_FALSE(Identity(0 <= partial_ordering::unordered));
00063   EXPECT_FALSE(Identity(partial_ordering::unordered > 0));
00064   EXPECT_FALSE(Identity(0 > partial_ordering::unordered));
00065   EXPECT_FALSE(Identity(partial_ordering::unordered >= 0));
00066   EXPECT_FALSE(Identity(0 >= partial_ordering::unordered));
00067 }
00068 
00069 TEST(Compare, WeakOrdering) {
00070   EXPECT_TRUE(Identity(weak_ordering::less < 0));
00071   EXPECT_TRUE(Identity(0 > weak_ordering::less));
00072   EXPECT_TRUE(Identity(weak_ordering::less <= 0));
00073   EXPECT_TRUE(Identity(0 >= weak_ordering::less));
00074   EXPECT_TRUE(Identity(weak_ordering::equivalent == 0));
00075   EXPECT_TRUE(Identity(0 == weak_ordering::equivalent));
00076   EXPECT_TRUE(Identity(weak_ordering::greater > 0));
00077   EXPECT_TRUE(Identity(0 < weak_ordering::greater));
00078   EXPECT_TRUE(Identity(weak_ordering::greater >= 0));
00079   EXPECT_TRUE(Identity(0 <= weak_ordering::greater));
00080 }
00081 
00082 TEST(Compare, StrongOrdering) {
00083   EXPECT_TRUE(Identity(strong_ordering::less < 0));
00084   EXPECT_TRUE(Identity(0 > strong_ordering::less));
00085   EXPECT_TRUE(Identity(strong_ordering::less <= 0));
00086   EXPECT_TRUE(Identity(0 >= strong_ordering::less));
00087   EXPECT_TRUE(Identity(strong_ordering::equal == 0));
00088   EXPECT_TRUE(Identity(0 == strong_ordering::equal));
00089   EXPECT_TRUE(Identity(strong_ordering::equivalent == 0));
00090   EXPECT_TRUE(Identity(0 == strong_ordering::equivalent));
00091   EXPECT_TRUE(Identity(strong_ordering::greater > 0));
00092   EXPECT_TRUE(Identity(0 < strong_ordering::greater));
00093   EXPECT_TRUE(Identity(strong_ordering::greater >= 0));
00094   EXPECT_TRUE(Identity(0 <= strong_ordering::greater));
00095 }
00096 
00097 TEST(Compare, Conversions) {
00098   EXPECT_TRUE(
00099       Identity(implicit_cast<weak_equality>(strong_equality::equal) == 0));
00100   EXPECT_TRUE(
00101       Identity(implicit_cast<weak_equality>(strong_equality::nonequal) != 0));
00102   EXPECT_TRUE(
00103       Identity(implicit_cast<weak_equality>(strong_equality::equivalent) == 0));
00104   EXPECT_TRUE(Identity(
00105       implicit_cast<weak_equality>(strong_equality::nonequivalent) != 0));
00106 
00107   EXPECT_TRUE(
00108       Identity(implicit_cast<weak_equality>(partial_ordering::less) != 0));
00109   EXPECT_TRUE(Identity(
00110       implicit_cast<weak_equality>(partial_ordering::equivalent) == 0));
00111   EXPECT_TRUE(
00112       Identity(implicit_cast<weak_equality>(partial_ordering::greater) != 0));
00113   EXPECT_TRUE(
00114       Identity(implicit_cast<weak_equality>(partial_ordering::unordered) != 0));
00115 
00116   EXPECT_TRUE(implicit_cast<weak_equality>(weak_ordering::less) != 0);
00117   EXPECT_TRUE(
00118       Identity(implicit_cast<weak_equality>(weak_ordering::equivalent) == 0));
00119   EXPECT_TRUE(
00120       Identity(implicit_cast<weak_equality>(weak_ordering::greater) != 0));
00121 
00122   EXPECT_TRUE(
00123       Identity(implicit_cast<partial_ordering>(weak_ordering::less) != 0));
00124   EXPECT_TRUE(
00125       Identity(implicit_cast<partial_ordering>(weak_ordering::less) < 0));
00126   EXPECT_TRUE(
00127       Identity(implicit_cast<partial_ordering>(weak_ordering::less) <= 0));
00128   EXPECT_TRUE(Identity(
00129       implicit_cast<partial_ordering>(weak_ordering::equivalent) == 0));
00130   EXPECT_TRUE(
00131       Identity(implicit_cast<partial_ordering>(weak_ordering::greater) != 0));
00132   EXPECT_TRUE(
00133       Identity(implicit_cast<partial_ordering>(weak_ordering::greater) > 0));
00134   EXPECT_TRUE(
00135       Identity(implicit_cast<partial_ordering>(weak_ordering::greater) >= 0));
00136 
00137   EXPECT_TRUE(
00138       Identity(implicit_cast<weak_equality>(strong_ordering::less) != 0));
00139   EXPECT_TRUE(
00140       Identity(implicit_cast<weak_equality>(strong_ordering::equal) == 0));
00141   EXPECT_TRUE(
00142       Identity(implicit_cast<weak_equality>(strong_ordering::equivalent) == 0));
00143   EXPECT_TRUE(
00144       Identity(implicit_cast<weak_equality>(strong_ordering::greater) != 0));
00145 
00146   EXPECT_TRUE(
00147       Identity(implicit_cast<strong_equality>(strong_ordering::less) != 0));
00148   EXPECT_TRUE(
00149       Identity(implicit_cast<strong_equality>(strong_ordering::equal) == 0));
00150   EXPECT_TRUE(Identity(
00151       implicit_cast<strong_equality>(strong_ordering::equivalent) == 0));
00152   EXPECT_TRUE(
00153       Identity(implicit_cast<strong_equality>(strong_ordering::greater) != 0));
00154 
00155   EXPECT_TRUE(
00156       Identity(implicit_cast<partial_ordering>(strong_ordering::less) != 0));
00157   EXPECT_TRUE(
00158       Identity(implicit_cast<partial_ordering>(strong_ordering::less) < 0));
00159   EXPECT_TRUE(
00160       Identity(implicit_cast<partial_ordering>(strong_ordering::less) <= 0));
00161   EXPECT_TRUE(
00162       Identity(implicit_cast<partial_ordering>(strong_ordering::equal) == 0));
00163   EXPECT_TRUE(Identity(
00164       implicit_cast<partial_ordering>(strong_ordering::equivalent) == 0));
00165   EXPECT_TRUE(
00166       Identity(implicit_cast<partial_ordering>(strong_ordering::greater) != 0));
00167   EXPECT_TRUE(
00168       Identity(implicit_cast<partial_ordering>(strong_ordering::greater) > 0));
00169   EXPECT_TRUE(
00170       Identity(implicit_cast<partial_ordering>(strong_ordering::greater) >= 0));
00171 
00172   EXPECT_TRUE(
00173       Identity(implicit_cast<weak_ordering>(strong_ordering::less) != 0));
00174   EXPECT_TRUE(
00175       Identity(implicit_cast<weak_ordering>(strong_ordering::less) < 0));
00176   EXPECT_TRUE(
00177       Identity(implicit_cast<weak_ordering>(strong_ordering::less) <= 0));
00178   EXPECT_TRUE(
00179       Identity(implicit_cast<weak_ordering>(strong_ordering::equal) == 0));
00180   EXPECT_TRUE(
00181       Identity(implicit_cast<weak_ordering>(strong_ordering::equivalent) == 0));
00182   EXPECT_TRUE(
00183       Identity(implicit_cast<weak_ordering>(strong_ordering::greater) != 0));
00184   EXPECT_TRUE(
00185       Identity(implicit_cast<weak_ordering>(strong_ordering::greater) > 0));
00186   EXPECT_TRUE(
00187       Identity(implicit_cast<weak_ordering>(strong_ordering::greater) >= 0));
00188 }
00189 
00190 struct WeakOrderingLess {
00191   template <typename T>
00192   absl::weak_ordering operator()(const T &a, const T &b) const {
00193     return a < b ? absl::weak_ordering::less
00194                  : a == b ? absl::weak_ordering::equivalent
00195                           : absl::weak_ordering::greater;
00196   }
00197 };
00198 
00199 TEST(CompareResultAsLessThan, SanityTest) {
00200   EXPECT_FALSE(absl::compare_internal::compare_result_as_less_than(false));
00201   EXPECT_TRUE(absl::compare_internal::compare_result_as_less_than(true));
00202 
00203   EXPECT_TRUE(
00204       absl::compare_internal::compare_result_as_less_than(weak_ordering::less));
00205   EXPECT_FALSE(absl::compare_internal::compare_result_as_less_than(
00206       weak_ordering::equivalent));
00207   EXPECT_FALSE(absl::compare_internal::compare_result_as_less_than(
00208       weak_ordering::greater));
00209 }
00210 
00211 TEST(DoLessThanComparison, SanityTest) {
00212   std::less<int> less;
00213   WeakOrderingLess weak;
00214 
00215   EXPECT_TRUE(absl::compare_internal::do_less_than_comparison(less, -1, 0));
00216   EXPECT_TRUE(absl::compare_internal::do_less_than_comparison(weak, -1, 0));
00217 
00218   EXPECT_FALSE(absl::compare_internal::do_less_than_comparison(less, 10, 10));
00219   EXPECT_FALSE(absl::compare_internal::do_less_than_comparison(weak, 10, 10));
00220 
00221   EXPECT_FALSE(absl::compare_internal::do_less_than_comparison(less, 10, 5));
00222   EXPECT_FALSE(absl::compare_internal::do_less_than_comparison(weak, 10, 5));
00223 }
00224 
00225 TEST(CompareResultAsOrdering, SanityTest) {
00226   EXPECT_TRUE(Identity(
00227       absl::compare_internal::compare_result_as_ordering(-1) < 0));
00228   EXPECT_FALSE(Identity(
00229       absl::compare_internal::compare_result_as_ordering(-1) == 0));
00230   EXPECT_FALSE(
00231       Identity(absl::compare_internal::compare_result_as_ordering(-1) > 0));
00232   EXPECT_TRUE(Identity(absl::compare_internal::compare_result_as_ordering(
00233                            weak_ordering::less) < 0));
00234   EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering(
00235                             weak_ordering::less) == 0));
00236   EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering(
00237                             weak_ordering::less) > 0));
00238 
00239   EXPECT_FALSE(Identity(
00240       absl::compare_internal::compare_result_as_ordering(0) < 0));
00241   EXPECT_TRUE(Identity(
00242       absl::compare_internal::compare_result_as_ordering(0) == 0));
00243   EXPECT_FALSE(Identity(
00244       absl::compare_internal::compare_result_as_ordering(0) > 0));
00245   EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering(
00246                            weak_ordering::equivalent) < 0));
00247   EXPECT_TRUE(Identity(absl::compare_internal::compare_result_as_ordering(
00248                             weak_ordering::equivalent) == 0));
00249   EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering(
00250                             weak_ordering::equivalent) > 0));
00251 
00252   EXPECT_FALSE(Identity(
00253       absl::compare_internal::compare_result_as_ordering(1) < 0));
00254   EXPECT_FALSE(Identity(
00255       absl::compare_internal::compare_result_as_ordering(1) == 0));
00256   EXPECT_TRUE(Identity(
00257       absl::compare_internal::compare_result_as_ordering(1) > 0));
00258   EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering(
00259                            weak_ordering::greater) < 0));
00260   EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering(
00261                             weak_ordering::greater) == 0));
00262   EXPECT_TRUE(Identity(absl::compare_internal::compare_result_as_ordering(
00263                             weak_ordering::greater) > 0));
00264 }
00265 
00266 TEST(DoThreeWayComparison, SanityTest) {
00267   std::less<int> less;
00268   WeakOrderingLess weak;
00269 
00270   EXPECT_TRUE(Identity(
00271       absl::compare_internal::do_three_way_comparison(less, -1, 0) < 0));
00272   EXPECT_FALSE(Identity(
00273       absl::compare_internal::do_three_way_comparison(less, -1, 0) == 0));
00274   EXPECT_FALSE(Identity(
00275       absl::compare_internal::do_three_way_comparison(less, -1, 0) > 0));
00276   EXPECT_TRUE(Identity(
00277       absl::compare_internal::do_three_way_comparison(weak, -1, 0) < 0));
00278   EXPECT_FALSE(Identity(
00279       absl::compare_internal::do_three_way_comparison(weak, -1, 0) == 0));
00280   EXPECT_FALSE(Identity(
00281       absl::compare_internal::do_three_way_comparison(weak, -1, 0) > 0));
00282 
00283   EXPECT_FALSE(Identity(
00284       absl::compare_internal::do_three_way_comparison(less, 10, 10) < 0));
00285   EXPECT_TRUE(Identity(
00286       absl::compare_internal::do_three_way_comparison(less, 10, 10) == 0));
00287   EXPECT_FALSE(Identity(
00288       absl::compare_internal::do_three_way_comparison(less, 10, 10) > 0));
00289   EXPECT_FALSE(Identity(
00290       absl::compare_internal::do_three_way_comparison(weak, 10, 10) < 0));
00291   EXPECT_TRUE(Identity(
00292       absl::compare_internal::do_three_way_comparison(weak, 10, 10) == 0));
00293   EXPECT_FALSE(Identity(
00294       absl::compare_internal::do_three_way_comparison(weak, 10, 10) > 0));
00295 
00296   EXPECT_FALSE(Identity(
00297       absl::compare_internal::do_three_way_comparison(less, 10, 5) < 0));
00298   EXPECT_FALSE(Identity(
00299       absl::compare_internal::do_three_way_comparison(less, 10, 5) == 0));
00300   EXPECT_TRUE(Identity(
00301       absl::compare_internal::do_three_way_comparison(less, 10, 5) > 0));
00302   EXPECT_FALSE(Identity(
00303       absl::compare_internal::do_three_way_comparison(weak, 10, 5) < 0));
00304   EXPECT_FALSE(Identity(
00305       absl::compare_internal::do_three_way_comparison(weak, 10, 5) == 0));
00306   EXPECT_TRUE(Identity(
00307       absl::compare_internal::do_three_way_comparison(weak, 10, 5) > 0));
00308 }
00309 
00310 }  // namespace
00311 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14