STT-tensorflow/tensorflow/compiler/xla/permutation_util_test.cc
Peter Hawkins 9fcf66e16d [XLA] Change xla::IsPermutation() not to check the size, and make its implementation O(n).
xla::IsPermutation() does two things: it checks a permutation is actually a permutation, and checks it has a particular size. There's no need to do the latter in xla::IsPermutation because it can be done just as easily by checking .size(), and a number of callers don't need the size check.

In addition, change the implementation to be O(n) time; there's no need to call std::is_permutation() which guarantees nothing better than O(n^2) behavior.

PiperOrigin-RevId: 356643750
Change-Id: Ib177287556b5c22266185fa813798a4ad5392054
2021-02-09 19:13:04 -08:00

36 lines
1.2 KiB
C++

/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/compiler/xla/permutation_util.h"
#include "tensorflow/compiler/xla/test.h"
namespace xla {
namespace {
TEST(PermutationUtilTest, IsPermutation) {
EXPECT_TRUE(IsPermutation({}));
EXPECT_TRUE(IsPermutation({0}));
EXPECT_FALSE(IsPermutation({-3}));
EXPECT_TRUE(IsPermutation({0, 1}));
EXPECT_FALSE(IsPermutation({1, 1}));
EXPECT_TRUE(IsPermutation({1, 0}));
EXPECT_TRUE(IsPermutation({3, 1, 0, 2}));
EXPECT_FALSE(IsPermutation({3, 0, 2}));
}
} // namespace
} // namespace xla