morphology_label_set_test.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright 2016 Google Inc. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include "syntaxnet/morphology_label_set.h"
  13. #include "syntaxnet/sentence.pb.h"
  14. #include <gmock/gmock.h>
  15. #include "tensorflow/core/lib/core/status.h"
  16. #include "tensorflow/core/platform/env.h"
  17. #include "tensorflow/core/platform/test.h"
  18. namespace syntaxnet {
  19. class MorphologyLabelSetTest : public ::testing::Test {
  20. protected:
  21. MorphologyLabelSet label_set_;
  22. };
  23. // Test that Add and LookupExisting work as expected.
  24. TEST_F(MorphologyLabelSetTest, AddLookupExisting) {
  25. TokenMorphology si1, si2; // singular, imperative
  26. TokenMorphology pi; // plural, imperative
  27. TokenMorphology six; // singular, imperative with extra value
  28. TextFormat::ParseFromString(R"(
  29. attribute {name: "Number" value: "Singular"}
  30. attribute {name: "POS" value: "IMP"})",
  31. &si1);
  32. TextFormat::ParseFromString(R"(
  33. attribute {name: "POS" value: "IMP"}
  34. attribute {name: "Number" value: "Singular"})",
  35. &si2);
  36. TextFormat::ParseFromString(R"(
  37. attribute {name: "Number" value: "Plural"}
  38. attribute {name: "POS" value: "IMP"})",
  39. &pi);
  40. TextFormat::ParseFromString(R"(
  41. attribute {name: "Number" value: "Plural"}
  42. attribute {name: "POS" value: "IMP"}
  43. attribute {name: "x" value: "x"})",
  44. &six);
  45. // Check Lookup existing returns -1 for non-existing entries.
  46. EXPECT_EQ(-1, label_set_.LookupExisting(si1));
  47. EXPECT_EQ(-1, label_set_.LookupExisting(si2));
  48. EXPECT_EQ(0, label_set_.Size());
  49. // Check that adding returns 0 (this is the only possiblity given Size())
  50. EXPECT_EQ(0, label_set_.Add(si1));
  51. EXPECT_EQ(0, label_set_.Add(si1)); // calling Add twice adds only once
  52. EXPECT_EQ(1, label_set_.Size());
  53. // Check that order of attributes does not matter.
  54. EXPECT_EQ(0, label_set_.LookupExisting(si2));
  55. // Check that un-added entries still are not present.
  56. EXPECT_EQ(-1, label_set_.LookupExisting(pi));
  57. EXPECT_EQ(-1, label_set_.LookupExisting(six));
  58. // Check that we can add them.
  59. EXPECT_EQ(1, label_set_.Add(pi));
  60. EXPECT_EQ(2, label_set_.Add(six));
  61. EXPECT_EQ(3, label_set_.Size());
  62. }
  63. // Test write and deserializing constructor.
  64. TEST_F(MorphologyLabelSetTest, Serialization) {
  65. TokenMorphology si; // singular, imperative
  66. TokenMorphology pi; // plural, imperative
  67. TextFormat::ParseFromString(R"(
  68. attribute {name: "Number" value: "Singular"}
  69. attribute {name: "POS" value: "IMP"})",
  70. &si);
  71. TextFormat::ParseFromString(R"(
  72. attribute {name: "Number" value: "Plural"}
  73. attribute {name: "POS" value: "IMP"})",
  74. &pi);
  75. EXPECT_EQ(0, label_set_.Add(si));
  76. EXPECT_EQ(1, label_set_.Add(pi));
  77. // Serialize and deserialize.
  78. string fname = utils::JoinPath({tensorflow::testing::TmpDir(), "label-set"});
  79. label_set_.Write(fname);
  80. MorphologyLabelSet label_set2(fname);
  81. EXPECT_EQ(0, label_set2.LookupExisting(si));
  82. EXPECT_EQ(1, label_set2.LookupExisting(pi));
  83. EXPECT_EQ(2, label_set2.Size());
  84. }
  85. } // namespace syntaxnet