Home | History | Annotate | Download | only in kernels
      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/framework/allocator.h"
     17 #include "tensorflow/core/framework/fake_input.h"
     18 #include "tensorflow/core/framework/node_def_builder.h"
     19 #include "tensorflow/core/framework/op_kernel.h"
     20 #include "tensorflow/core/framework/tensor.h"
     21 #include "tensorflow/core/framework/tensor_testutil.h"
     22 #include "tensorflow/core/framework/types.h"
     23 #include "tensorflow/core/framework/types.pb.h"
     24 #include "tensorflow/core/kernels/ops_testutil.h"
     25 #include "tensorflow/core/kernels/ops_util.h"
     26 #include "tensorflow/core/lib/core/status_test_util.h"
     27 #include "tensorflow/core/platform/test.h"
     28 
     29 namespace tensorflow {
     30 
     31 class CrossOpTest : public OpsTestBase {
     32  protected:
     33   CrossOpTest() {
     34     TF_EXPECT_OK(NodeDefBuilder("cross_op", "Cross")
     35                      .Input(FakeInput(DT_FLOAT))
     36                      .Input(FakeInput(DT_FLOAT))
     37                      .Finalize(node_def()));
     38     TF_EXPECT_OK(InitOp());
     39   }
     40 };
     41 
     42 TEST_F(CrossOpTest, Zero) {
     43   AddInputFromArray<float>(TensorShape({3}), {0, 0, 0});
     44   AddInputFromArray<float>(TensorShape({3}), {0, 0, 0});
     45   TF_ASSERT_OK(RunOpKernel());
     46 
     47   Tensor expected(allocator(), DT_FLOAT, TensorShape({3}));
     48   test::FillValues<float>(&expected, {0, 0, 0});
     49   test::ExpectTensorEqual<float>(expected, *GetOutput(0));
     50 }
     51 
     52 TEST_F(CrossOpTest, RightHandRule) {
     53   AddInputFromArray<float>(TensorShape({2, 3}), {1, 0, 0, /**/ 0, 1, 0});
     54   AddInputFromArray<float>(TensorShape({2, 3}), {0, 1, 0, /**/ 1, 0, 0});
     55   TF_ASSERT_OK(RunOpKernel());
     56 
     57   Tensor expected(allocator(), DT_FLOAT, TensorShape({2, 3}));
     58   test::FillValues<float>(&expected, {{0, 0, 1, /**/ 0, 0, -1}});
     59   test::ExpectTensorEqual<float>(expected, *GetOutput(0));
     60 }
     61 
     62 TEST_F(CrossOpTest, ArbitraryNonintegral) {
     63   const float u1 = -0.669, u2 = -0.509, u3 = 0.125;
     64   const float v1 = -0.477, v2 = 0.592, v3 = -0.110;
     65   const float s1 = u2 * v3 - u3 * v2;
     66   const float s2 = u3 * v1 - u1 * v3;
     67   const float s3 = u1 * v2 - u2 * v1;
     68 
     69   AddInputFromArray<float>(TensorShape({3}), {u1, u2, u3});
     70   AddInputFromArray<float>(TensorShape({3}), {v1, v2, v3});
     71   TF_ASSERT_OK(RunOpKernel());
     72 
     73   Tensor expected(allocator(), DT_FLOAT, TensorShape({3}));
     74   test::FillValues<float>(&expected, {s1, s2, s3});
     75   test::ExpectTensorNear<float>(expected, *GetOutput(0), 1e-6);
     76 }
     77 
     78 class CrossOpIntTest : public OpsTestBase {
     79  protected:
     80   CrossOpIntTest() {
     81     TF_EXPECT_OK(NodeDefBuilder("cross_int_op", "Cross")
     82                      .Input(FakeInput(DT_INT32))
     83                      .Input(FakeInput(DT_INT32))
     84                      .Finalize(node_def()));
     85     TF_EXPECT_OK(InitOp());
     86   }
     87 };
     88 
     89 TEST_F(CrossOpIntTest, RightHandRule) {
     90   AddInputFromArray<int>(TensorShape({2, 3}), {2, 0, 0, /**/ 0, 2, 0});
     91   AddInputFromArray<int>(TensorShape({2, 3}), {0, 2, 0, /**/ 2, 0, 0});
     92   TF_ASSERT_OK(RunOpKernel());
     93 
     94   Tensor expected(allocator(), DT_INT32, TensorShape({2, 3}));
     95   test::FillValues<int>(&expected, {{0, 0, 4, /**/ 0, 0, -4}});
     96   test::ExpectTensorEqual<int>(expected, *GetOutput(0));
     97 }
     98 
     99 }  // namespace tensorflow
    100