Home | History | Annotate | Download | only in core
      1 /* Copyright 2015 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 #include "tensorflow/core/lib/core/status.h"
     17 #include "tensorflow/core/lib/core/errors.h"
     18 #include "tensorflow/core/lib/core/status_test_util.h"
     19 #include "tensorflow/core/platform/test.h"
     20 #include "tensorflow/core/platform/test_benchmark.h"
     21 
     22 namespace tensorflow {
     23 
     24 TEST(Status, OK) {
     25   EXPECT_EQ(Status::OK().code(), error::OK);
     26   EXPECT_EQ(Status::OK().error_message(), "");
     27   TF_EXPECT_OK(Status::OK());
     28   TF_ASSERT_OK(Status::OK());
     29   EXPECT_EQ(Status::OK(), Status());
     30   Status s;
     31   EXPECT_TRUE(s.ok());
     32 }
     33 
     34 TEST(DeathStatus, CheckOK) {
     35   Status status(errors::InvalidArgument("Invalid"));
     36   ASSERT_DEATH(TF_CHECK_OK(status), "Invalid");
     37 }
     38 
     39 TEST(Status, Set) {
     40   Status status;
     41   status = Status(error::CANCELLED, "Error message");
     42   EXPECT_EQ(status.code(), error::CANCELLED);
     43   EXPECT_EQ(status.error_message(), "Error message");
     44 }
     45 
     46 TEST(Status, Copy) {
     47   Status a(errors::InvalidArgument("Invalid"));
     48   Status b(a);
     49   ASSERT_EQ(a.ToString(), b.ToString());
     50 }
     51 
     52 TEST(Status, Assign) {
     53   Status a(errors::InvalidArgument("Invalid"));
     54   Status b;
     55   b = a;
     56   ASSERT_EQ(a.ToString(), b.ToString());
     57 }
     58 
     59 TEST(Status, Update) {
     60   Status s;
     61   s.Update(Status::OK());
     62   ASSERT_TRUE(s.ok());
     63   Status a(errors::InvalidArgument("Invalid"));
     64   s.Update(a);
     65   ASSERT_EQ(s.ToString(), a.ToString());
     66   Status b(errors::Internal("Internal"));
     67   s.Update(b);
     68   ASSERT_EQ(s.ToString(), a.ToString());
     69   s.Update(Status::OK());
     70   ASSERT_EQ(s.ToString(), a.ToString());
     71   ASSERT_FALSE(s.ok());
     72 }
     73 
     74 TEST(Status, EqualsOK) { ASSERT_EQ(Status::OK(), Status()); }
     75 
     76 TEST(Status, EqualsSame) {
     77   Status a(errors::InvalidArgument("Invalid"));
     78   Status b(errors::InvalidArgument("Invalid"));
     79   ASSERT_EQ(a, b);
     80 }
     81 
     82 TEST(Status, EqualsCopy) {
     83   const Status a(errors::InvalidArgument("Invalid"));
     84   const Status b = a;
     85   ASSERT_EQ(a, b);
     86 }
     87 
     88 TEST(Status, EqualsDifferentCode) {
     89   const Status a(errors::InvalidArgument("message"));
     90   const Status b(errors::Internal("message"));
     91   ASSERT_NE(a, b);
     92 }
     93 
     94 TEST(Status, EqualsDifferentMessage) {
     95   const Status a(errors::InvalidArgument("message"));
     96   const Status b(errors::InvalidArgument("another"));
     97   ASSERT_NE(a, b);
     98 }
     99 
    100 static void BM_TF_CHECK_OK(int iters) {
    101   tensorflow::Status s =
    102       (iters < 0) ? errors::InvalidArgument("Invalid") : Status::OK();
    103   for (int i = 0; i < iters; i++) {
    104     TF_CHECK_OK(s);
    105   }
    106 }
    107 BENCHMARK(BM_TF_CHECK_OK);
    108 
    109 }  // namespace tensorflow
    110