File: XPathFunctionsTest.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (126 lines) | stat: -rw-r--r-- 4,677 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "core/xml/XPathFunctions.h"

#include "core/dom/Document.h"
#include "core/xml/XPathExpressionNode.h"  // EvaluationContext
#include "core/xml/XPathPredicate.h"       // Number, StringExpression
#include "core/xml/XPathValue.h"
#include "platform/heap/Handle.h"  // HeapVector, Member, etc.
#include "testing/gtest/include/gtest/gtest.h"
#include "wtf/Allocator.h"

#include <cmath>
#include <limits>

namespace blink {

namespace {

class XPathContext {
  STACK_ALLOCATED();

 public:
  XPathContext() : m_document(Document::create()), m_context(*m_document) {}

  XPath::EvaluationContext& context() { return m_context; }
  Document& document() { return *m_document; }

 private:
  const Member<Document> m_document;
  XPath::EvaluationContext m_context;
};

using XPathArguments = HeapVector<Member<XPath::Expression>>;

static String substring(XPathArguments& args) {
  XPathContext xpath;
  XPath::Expression* call = XPath::createFunction("substring", args);
  XPath::Value result = call->evaluate(xpath.context());
  return result.toString();
}

static String substring(const char* string, double pos) {
  XPathArguments args;
  args.push_back(new XPath::StringExpression(string));
  args.push_back(new XPath::Number(pos));
  return substring(args);
}

static String substring(const char* string, double pos, double len) {
  XPathArguments args;
  args.push_back(new XPath::StringExpression(string));
  args.push_back(new XPath::Number(pos));
  args.push_back(new XPath::Number(len));
  return substring(args);
}

}  // namespace

TEST(XPathFunctionsTest, substring_specExamples) {
  EXPECT_EQ(" car", substring("motor car", 6.0))
      << "should select characters staring at position 6 to the end";
  EXPECT_EQ("ada", substring("metadata", 4.0, 3.0))
      << "should select characters at 4 <= position < 7";
  EXPECT_EQ("234", substring("123456", 1.5, 2.6))
      << "should select characters at 2 <= position < 5";
  EXPECT_EQ("12", substring("12345", 0.0, 3.0))
      << "should select characters at 0 <= position < 3; note the first "
         "position is 1 so this is characters in position 1 and 2";
  EXPECT_EQ("", substring("12345", 5.0, -3.0))
      << "no characters should have 5 <= position < 2";
  EXPECT_EQ("1", substring("12345", -3.0, 5.0))
      << "should select characters at -3 <= position < 2; since the first "
         "position is 1, this is the character at position 1";
  EXPECT_EQ("", substring("12345", NAN, 3.0))
      << "should select no characters since NaN <= position is always false";
  EXPECT_EQ("", substring("12345", 1.0, NAN))
      << "should select no characters since position < 1. + NaN is always "
         "false";
  EXPECT_EQ("12345",
            substring("12345", -42, std::numeric_limits<double>::infinity()))
      << "should select characters at -42 <= position < Infinity, which is all "
         "of them";
  EXPECT_EQ("", substring("12345", -std::numeric_limits<double>::infinity(),
                          std::numeric_limits<double>::infinity()))
      << "since -Inf+Inf is NaN, should select no characters since position "
         "< NaN is always false";
}

TEST(XPathFunctionsTest, substring_emptyString) {
  EXPECT_EQ("", substring("", 0.0, 1.0))
      << "substring of an empty string should be the empty string";
}

TEST(XPathFunctionsTest, substring) {
  EXPECT_EQ("hello", substring("well hello there", 6.0, 5.0));
}

TEST(XPathFunctionsTest, substring_negativePosition) {
  EXPECT_EQ("hello", substring("hello, world!", -4.0, 10.0))
      << "negative start positions should impinge on the result length";
  // Try to underflow the length adjustment for negative positions.
  EXPECT_EQ("", substring("hello", std::numeric_limits<long>::min() + 1, 1.0));
}

TEST(XPathFunctionsTest, substring_negativeLength) {
  EXPECT_EQ("", substring("hello, world!", 1.0, -3.0))
      << "negative lengths should result in an empty string";

  EXPECT_EQ("", substring("foo", std::numeric_limits<long>::min(), 1.0))
      << "large (but long representable) negative position should result in "
      << "an empty string";
}

TEST(XPathFunctionsTest, substring_extremePositionLength) {
  EXPECT_EQ("", substring("no way", 1e100, 7.0))
      << "extremely large positions should result in the empty string";

  EXPECT_EQ("no way", substring("no way", -1e200, 1e300))
      << "although these indices are not representable as long, this should "
      << "produce the string because indices are computed as doubles";
}

}  // namespace blink