tor-browser

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

bind_test.cc (5708B)


      1 // Copyright 2020 The Abseil Authors.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //      https://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 #include "absl/strings/internal/str_format/bind.h"
     16 
     17 #include <string.h>
     18 #include <limits>
     19 
     20 #include "gtest/gtest.h"
     21 
     22 namespace absl {
     23 ABSL_NAMESPACE_BEGIN
     24 namespace str_format_internal {
     25 namespace {
     26 
     27 class FormatBindTest : public ::testing::Test {
     28 public:
     29  bool Extract(const char *s, UnboundConversion *props, int *next) const {
     30    return ConsumeUnboundConversion(s, s + strlen(s), props, next) ==
     31           s + strlen(s);
     32  }
     33 };
     34 
     35 TEST_F(FormatBindTest, BindSingle) {
     36  struct Expectation {
     37    int line;
     38    const char *fmt;
     39    int ok_phases;
     40    const FormatArgImpl *arg;
     41    int width;
     42    int precision;
     43    int next_arg;
     44  };
     45  const int no = -1;
     46  const int ia[] = { 10, 20, 30, 40};
     47  const FormatArgImpl args[] = {FormatArgImpl(ia[0]), FormatArgImpl(ia[1]),
     48                                FormatArgImpl(ia[2]), FormatArgImpl(ia[3])};
     49 #pragma GCC diagnostic push
     50 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
     51  const Expectation kExpect[] = {
     52    {__LINE__, "d",          2, &args[0], no, no, 2},
     53    {__LINE__, "4d",         2, &args[0],  4, no, 2},
     54    {__LINE__, ".5d",        2, &args[0], no,  5, 2},
     55    {__LINE__, "4.5d",       2, &args[0],  4,  5, 2},
     56    {__LINE__, "*d",         2, &args[1], 10, no, 3},
     57    {__LINE__, ".*d",        2, &args[1], no, 10, 3},
     58    {__LINE__, "*.*d",       2, &args[2], 10, 20, 4},
     59    {__LINE__, "1$d",        2, &args[0], no, no, 0},
     60    {__LINE__, "2$d",        2, &args[1], no, no, 0},
     61    {__LINE__, "3$d",        2, &args[2], no, no, 0},
     62    {__LINE__, "4$d",        2, &args[3], no, no, 0},
     63    {__LINE__, "2$*1$d",     2, &args[1], 10, no, 0},
     64    {__LINE__, "2$*2$d",     2, &args[1], 20, no, 0},
     65    {__LINE__, "2$*3$d",     2, &args[1], 30, no, 0},
     66    {__LINE__, "2$.*1$d",    2, &args[1], no, 10, 0},
     67    {__LINE__, "2$.*2$d",    2, &args[1], no, 20, 0},
     68    {__LINE__, "2$.*3$d",    2, &args[1], no, 30, 0},
     69    {__LINE__, "2$*3$.*1$d", 2, &args[1], 30, 10, 0},
     70    {__LINE__, "2$*2$.*2$d", 2, &args[1], 20, 20, 0},
     71    {__LINE__, "2$*1$.*3$d", 2, &args[1], 10, 30, 0},
     72    {__LINE__, "2$*3$.*1$d", 2, &args[1], 30, 10, 0},
     73    {__LINE__, "1$*d",       0},  // indexed, then positional
     74    {__LINE__, "*2$d",       0},  // positional, then indexed
     75    {__LINE__, "6$d",        1},  // arg position out of bounds
     76    {__LINE__, "1$6$d",      0},  // width position incorrectly specified
     77    {__LINE__, "1$.6$d",     0},  // precision position incorrectly specified
     78    {__LINE__, "1$*6$d",     1},  // width position out of bounds
     79    {__LINE__, "1$.*6$d",    1},  // precision position out of bounds
     80  };
     81 #pragma GCC diagnostic pop
     82  for (const Expectation &e : kExpect) {
     83    SCOPED_TRACE(e.line);
     84    SCOPED_TRACE(e.fmt);
     85    UnboundConversion props;
     86    BoundConversion bound;
     87    int ok_phases = 0;
     88    int next = 0;
     89    if (Extract(e.fmt, &props, &next)) {
     90      ++ok_phases;
     91      if (BindWithPack(&props, args, &bound)) {
     92        ++ok_phases;
     93      }
     94    }
     95    EXPECT_EQ(e.ok_phases, ok_phases);
     96    if (e.ok_phases < 2) continue;
     97    if (e.arg != nullptr) {
     98      EXPECT_EQ(e.arg, bound.arg());
     99    }
    100    EXPECT_EQ(e.width, bound.width());
    101    EXPECT_EQ(e.precision, bound.precision());
    102  }
    103 }
    104 
    105 TEST_F(FormatBindTest, WidthUnderflowRegression) {
    106  UnboundConversion props;
    107  BoundConversion bound;
    108  int next = 0;
    109  const int args_i[] = {std::numeric_limits<int>::min(), 17};
    110  const FormatArgImpl args[] = {FormatArgImpl(args_i[0]),
    111                                FormatArgImpl(args_i[1])};
    112  ASSERT_TRUE(Extract("*d", &props, &next));
    113  ASSERT_TRUE(BindWithPack(&props, args, &bound));
    114 
    115  EXPECT_EQ(bound.width(), std::numeric_limits<int>::max());
    116  EXPECT_EQ(bound.arg(), args + 1);
    117 }
    118 
    119 TEST_F(FormatBindTest, FormatPack) {
    120  struct Expectation {
    121    int line;
    122    const char *fmt;
    123    const char *summary;
    124  };
    125  const int ia[] = { 10, 20, 30, 40, -10 };
    126  const FormatArgImpl args[] = {FormatArgImpl(ia[0]), FormatArgImpl(ia[1]),
    127                                FormatArgImpl(ia[2]), FormatArgImpl(ia[3]),
    128                                FormatArgImpl(ia[4])};
    129  const Expectation kExpect[] = {
    130      {__LINE__, "a%4db%dc", "a{10:4d}b{20:d}c"},
    131      {__LINE__, "a%.4db%dc", "a{10:.4d}b{20:d}c"},
    132      {__LINE__, "a%4.5db%dc", "a{10:4.5d}b{20:d}c"},
    133      {__LINE__, "a%db%4.5dc", "a{10:d}b{20:4.5d}c"},
    134      {__LINE__, "a%db%*.*dc", "a{10:d}b{40:20.30d}c"},
    135      {__LINE__, "a%.*fb", "a{20:.10f}b"},
    136      {__LINE__, "a%1$db%2$*3$.*4$dc", "a{10:d}b{20:30.40d}c"},
    137      {__LINE__, "a%4$db%3$*2$.*1$dc", "a{40:d}b{30:20.10d}c"},
    138      {__LINE__, "a%04ldb", "a{10:04d}b"},
    139      {__LINE__, "a%-#04lldb", "a{10:-#04d}b"},
    140      {__LINE__, "a%1$*5$db", "a{10:-10d}b"},
    141      {__LINE__, "a%1$.*5$db", "a{10:d}b"},
    142  };
    143  for (const Expectation &e : kExpect) {
    144    absl::string_view fmt = e.fmt;
    145    SCOPED_TRACE(e.line);
    146    SCOPED_TRACE(e.fmt);
    147    UntypedFormatSpecImpl format(fmt);
    148    EXPECT_EQ(e.summary,
    149              str_format_internal::Summarize(format, absl::MakeSpan(args)))
    150        << "line:" << e.line;
    151  }
    152 }
    153 
    154 }  // namespace
    155 }  // namespace str_format_internal
    156 ABSL_NAMESPACE_END
    157 }  // namespace absl