string_piece.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  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. // http://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. // Author: jdtang@google.com (Jonathan Tang)
  16. #include "string_piece.h"
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "gtest/gtest.h"
  20. #include "parser.h"
  21. #include "test_utils.h"
  22. #include "util.h"
  23. namespace {
  24. typedef GumboTest GumboStringPieceTest;
  25. #define INIT_GUMBO_STRING(varname, literal) \
  26. GumboStringPiece varname = {literal, sizeof(literal) - 1}
  27. TEST_F(GumboStringPieceTest, Equal) {
  28. INIT_GUMBO_STRING(str1, "foo");
  29. INIT_GUMBO_STRING(str2, "foo");
  30. EXPECT_TRUE(gumbo_string_equals(&str1, &str2));
  31. }
  32. TEST_F(GumboStringPieceTest, NotEqual_DifferingCase) {
  33. INIT_GUMBO_STRING(str1, "foo");
  34. INIT_GUMBO_STRING(str2, "Foo");
  35. EXPECT_FALSE(gumbo_string_equals(&str1, &str2));
  36. }
  37. TEST_F(GumboStringPieceTest, NotEqual_Str1Shorter) {
  38. INIT_GUMBO_STRING(str1, "foo");
  39. INIT_GUMBO_STRING(str2, "foobar");
  40. EXPECT_FALSE(gumbo_string_equals(&str1, &str2));
  41. }
  42. TEST_F(GumboStringPieceTest, NotEqual_Str2Shorter) {
  43. INIT_GUMBO_STRING(str1, "foobar");
  44. INIT_GUMBO_STRING(str2, "foo");
  45. EXPECT_FALSE(gumbo_string_equals(&str1, &str2));
  46. }
  47. TEST_F(GumboStringPieceTest, NotEqual_DifferentText) {
  48. INIT_GUMBO_STRING(str1, "bar");
  49. INIT_GUMBO_STRING(str2, "foo");
  50. EXPECT_FALSE(gumbo_string_equals(&str1, &str2));
  51. }
  52. TEST_F(GumboStringPieceTest, CaseEqual) {
  53. INIT_GUMBO_STRING(str1, "foo");
  54. INIT_GUMBO_STRING(str2, "fOO");
  55. EXPECT_TRUE(gumbo_string_equals_ignore_case(&str1, &str2));
  56. }
  57. TEST_F(GumboStringPieceTest, CaseNotEqual_Str2Shorter) {
  58. INIT_GUMBO_STRING(str1, "foobar");
  59. INIT_GUMBO_STRING(str2, "foo");
  60. EXPECT_FALSE(gumbo_string_equals_ignore_case(&str1, &str2));
  61. }
  62. TEST_F(GumboStringPieceTest, Copy) {
  63. GumboParser parser;
  64. parser._options = &kGumboDefaultOptions;
  65. INIT_GUMBO_STRING(str1, "bar");
  66. GumboStringPiece str2;
  67. gumbo_string_copy(&parser, &str2, &str1);
  68. EXPECT_TRUE(gumbo_string_equals(&str1, &str2));
  69. gumbo_parser_deallocate(&parser, (void*) str2.data);
  70. }
  71. } // namespace