Home | History | Annotate | Download | only in tests
      1 /* Copyright 2017 The TensorFlow Authors. 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 
     16 // Tests that our utility functions for dealing with literals are correctly
     17 // implemented.
     18 
     19 #include "tensorflow/compiler/xla/tests/literal_test_util.h"
     20 
     21 #include <vector>
     22 
     23 #include "tensorflow/compiler/xla/test_helpers.h"
     24 #include "tensorflow/core/lib/io/path.h"
     25 #include "tensorflow/core/lib/strings/str_util.h"
     26 #include "tensorflow/core/platform/env.h"
     27 #include "tensorflow/core/platform/logging.h"
     28 #include "tensorflow/core/platform/test.h"
     29 
     30 namespace xla {
     31 namespace {
     32 
     33 TEST(LiteralTestUtilTest, ComparesEqualTuplesEqual) {
     34   std::unique_ptr<Literal> literal = Literal::MakeTuple({
     35       Literal::CreateR0<int32>(42).get(), Literal::CreateR0<int32>(64).get(),
     36   });
     37   LiteralTestUtil::ExpectEqual(*literal, *literal);
     38 }
     39 
     40 TEST(LiteralTestUtilTest, ComparesUnequalTuplesUnequal) {
     41   // Implementation note: we have to use a death test here, because you can't
     42   // un-fail an assertion failure. The CHECK-failure is death, so we can make a
     43   // death assertion.
     44   auto unequal_things_are_equal = [] {
     45     std::unique_ptr<Literal> lhs = Literal::MakeTuple({
     46         Literal::CreateR0<int32>(42).get(), Literal::CreateR0<int32>(64).get(),
     47     });
     48     std::unique_ptr<Literal> rhs = Literal::MakeTuple({
     49         Literal::CreateR0<int32>(64).get(), Literal::CreateR0<int32>(42).get(),
     50     });
     51     CHECK(LiteralTestUtil::Equal(*lhs, *rhs)) << "LHS and RHS are unequal";
     52   };
     53   ASSERT_DEATH(unequal_things_are_equal(), "LHS and RHS are unequal");
     54 }
     55 
     56 TEST(LiteralTestUtilTest, ExpectNearFailurePlacesResultsInTemporaryDirectory) {
     57   auto dummy_lambda = [] {
     58     auto two = Literal::CreateR0<float>(2);
     59     auto four = Literal::CreateR0<float>(4);
     60     ErrorSpec error(0.001);
     61     CHECK(LiteralTestUtil::Near(*two, *four, error)) << "two is not near four";
     62   };
     63 
     64   tensorflow::Env* env = tensorflow::Env::Default();
     65   string pattern =
     66       tensorflow::io::JoinPath(tensorflow::testing::TmpDir(), "/tempfile-*");
     67   std::vector<string> files;
     68   TF_CHECK_OK(env->GetMatchingPaths(pattern, &files));
     69   for (const auto& f : files) {
     70     TF_CHECK_OK(env->DeleteFile(f)) << f;
     71   }
     72 
     73   ASSERT_DEATH(dummy_lambda(), "two is not near four");
     74 
     75   // Now check we wrote temporary files to the temporary directory that we can
     76   // read.
     77   std::vector<string> results;
     78   TF_CHECK_OK(env->GetMatchingPaths(pattern, &results));
     79 
     80   LOG(INFO) << "results: [" << tensorflow::str_util::Join(results, ", ") << "]";
     81   EXPECT_EQ(3, results.size());
     82   for (const string& result : results) {
     83     LiteralProto literal_proto;
     84     TF_CHECK_OK(tensorflow::ReadBinaryProto(tensorflow::Env::Default(), result,
     85                                             &literal_proto));
     86     std::unique_ptr<Literal> literal =
     87         Literal::CreateFromProto(literal_proto).ConsumeValueOrDie();
     88     if (result.find("expected") != string::npos) {
     89       EXPECT_EQ("2", literal->ToString());
     90     } else if (result.find("actual") != string::npos) {
     91       EXPECT_EQ("4", literal->ToString());
     92     } else if (result.find("miscompares") != string::npos) {
     93       EXPECT_EQ("true", literal->ToString());
     94     } else {
     95       FAIL() << "unknown file in temporary directory: " << result;
     96     }
     97   }
     98 }
     99 
    100 TEST(LiteralTestUtilTest, NearComparatorR1) {
    101   auto a =
    102       Literal::CreateR1<float>({0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8});
    103   auto b =
    104       Literal::CreateR1<float>({0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8});
    105   EXPECT_TRUE(LiteralTestUtil::Near(*a, *b, ErrorSpec{0.0001}));
    106 }
    107 
    108 TEST(LiteralTestUtilTest, NearComparatorR1Nan) {
    109   auto a =
    110       Literal::CreateR1<float>({0.0, 0.1, 0.2, 0.3, NAN, 0.5, 0.6, 0.7, 0.8});
    111   auto b =
    112       Literal::CreateR1<float>({0.0, 0.1, 0.2, 0.3, NAN, 0.5, 0.6, 0.7, 0.8});
    113   EXPECT_TRUE(LiteralTestUtil::Near(*a, *b, ErrorSpec{0.0001}));
    114 }
    115 
    116 TEST(LiteralTestUtil, NearComparatorDifferentLengths) {
    117   auto a =
    118       Literal::CreateR1<float>({0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8});
    119   auto b = Literal::CreateR1<float>({0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7});
    120   EXPECT_FALSE(LiteralTestUtil::Near(*a, *b, ErrorSpec{0.0001}));
    121   EXPECT_FALSE(LiteralTestUtil::Near(*b, *a, ErrorSpec{0.0001}));
    122 }
    123 
    124 }  // namespace
    125 }  // namespace xla
    126