OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ios/chrome/browser/ui/tab_switcher/tab_switcher_utils.h" |
| 6 |
| 7 #include "testing/platform_test.h" |
| 8 |
| 9 class TabSwitcherUtilsTest : public PlatformTest { |
| 10 protected: |
| 11 // Checks that the computed updates/deletions/insertions to go from |initial| |
| 12 // to |final| correctly match the expected updates/deletions/insertions. |
| 13 void TestLevenshtein(std::vector<size_t> const& initial, |
| 14 std::vector<size_t> const& final, |
| 15 std::vector<size_t> const& expectedSubstitutions, |
| 16 std::vector<size_t> const& expectedDeletions, |
| 17 std::vector<size_t> const& expectedInsertions) { |
| 18 std::vector<size_t> substitutions; |
| 19 std::vector<size_t> deletions; |
| 20 std::vector<size_t> insertions; |
| 21 ios_internal::MinimalReplacementOperations(initial, final, &substitutions, |
| 22 &deletions, &insertions); |
| 23 EXPECT_EQ(substitutions, expectedSubstitutions); |
| 24 EXPECT_EQ(deletions, expectedDeletions); |
| 25 EXPECT_EQ(insertions, expectedInsertions); |
| 26 } |
| 27 }; |
| 28 |
| 29 TEST_F(TabSwitcherUtilsTest, TestLevenshteinEmptyVectors) { |
| 30 std::vector<size_t> initial = {}; |
| 31 std::vector<size_t> final = {}; |
| 32 std::vector<size_t> expectedSubstitutions = {}; |
| 33 std::vector<size_t> expectedDeletions = {}; |
| 34 std::vector<size_t> expectedInsertions = {}; |
| 35 TestLevenshtein(initial, final, expectedSubstitutions, expectedDeletions, |
| 36 expectedInsertions); |
| 37 } |
| 38 |
| 39 TEST_F(TabSwitcherUtilsTest, TestLevenshteinNoChange) { |
| 40 std::vector<size_t> initial = {1, 2, 3}; |
| 41 std::vector<size_t> final = {1, 2, 3}; |
| 42 std::vector<size_t> expectedSubstitutions = {}; |
| 43 std::vector<size_t> expectedDeletions = {}; |
| 44 std::vector<size_t> expectedInsertions = {}; |
| 45 TestLevenshtein(initial, final, expectedSubstitutions, expectedDeletions, |
| 46 expectedInsertions); |
| 47 } |
| 48 |
| 49 TEST_F(TabSwitcherUtilsTest, TestLevenshteinInsertions) { |
| 50 std::vector<size_t> initial = {1}; |
| 51 std::vector<size_t> final = {0, 1, 2}; |
| 52 std::vector<size_t> expectedSubstitutions = {}; |
| 53 std::vector<size_t> expectedDeletions = {}; |
| 54 std::vector<size_t> expectedInsertions = {0, 2}; |
| 55 TestLevenshtein(initial, final, expectedSubstitutions, expectedDeletions, |
| 56 expectedInsertions); |
| 57 } |
| 58 |
| 59 TEST_F(TabSwitcherUtilsTest, TestLevenshteinDeletions) { |
| 60 std::vector<size_t> initial = {0, 1, 2, 3, 4, 5}; |
| 61 std::vector<size_t> final = {0, 2, 3, 5}; |
| 62 std::vector<size_t> expectedSubstitutions = {}; |
| 63 std::vector<size_t> expectedDeletions = {1, 4}; |
| 64 std::vector<size_t> expectedInsertions = {}; |
| 65 TestLevenshtein(initial, final, expectedSubstitutions, expectedDeletions, |
| 66 expectedInsertions); |
| 67 } |
| 68 |
| 69 TEST_F(TabSwitcherUtilsTest, TestLevenshteinFromAndToEmptyVectors) { |
| 70 std::vector<size_t> empty = {}; |
| 71 std::vector<size_t> nonEmpty = {0, 1, 2}; |
| 72 std::vector<size_t> expectedSubstitutions = {}; |
| 73 std::vector<size_t> expectedDeletions = {}; |
| 74 std::vector<size_t> expectedInsertions = {0, 1, 2}; |
| 75 TestLevenshtein(empty, nonEmpty, expectedSubstitutions, expectedDeletions, |
| 76 expectedInsertions); |
| 77 |
| 78 // Tests the reverse transformation. |
| 79 TestLevenshtein(nonEmpty, empty, expectedSubstitutions, expectedInsertions, |
| 80 expectedDeletions); |
| 81 } |
| 82 |
| 83 TEST_F(TabSwitcherUtilsTest, TestLevenshteinSubstitutions) { |
| 84 std::vector<size_t> initial = {0, 1, 2, 3, 4, 5, 6}; |
| 85 std::vector<size_t> final = {0, 1, 2, 9, 8, 5, 6}; |
| 86 std::vector<size_t> expectedSubstitutions = {3, 4}; |
| 87 std::vector<size_t> expectedDeletions = {}; |
| 88 std::vector<size_t> expectedInsertions = {}; |
| 89 TestLevenshtein(initial, final, expectedSubstitutions, expectedDeletions, |
| 90 expectedInsertions); |
| 91 } |
| 92 |
| 93 TEST_F(TabSwitcherUtilsTest, TestLevenshteinInsertionAndDeletions) { |
| 94 std::vector<size_t> initial = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 95 std::vector<size_t> final = {0, 1, 8, 9, 2, 3, 4, 7}; |
| 96 std::vector<size_t> expectedSubstitutions = {}; |
| 97 std::vector<size_t> expectedDeletions = {5, 6}; |
| 98 std::vector<size_t> expectedInsertions = {2, 3}; |
| 99 TestLevenshtein(initial, final, expectedSubstitutions, expectedDeletions, |
| 100 expectedInsertions); |
| 101 |
| 102 // Tests the reverse transformation. |
| 103 TestLevenshtein(final, initial, expectedSubstitutions, expectedInsertions, |
| 104 expectedDeletions); |
| 105 } |
| 106 |
| 107 TEST_F(TabSwitcherUtilsTest, TestLevenshteinInsertionAndSubstitutions) { |
| 108 std::vector<size_t> initial = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 109 std::vector<size_t> final = {0, 1, 2, 99, 98, 3, 4, 97, 96, 7}; |
| 110 std::vector<size_t> expectedSubstitutions = {5, 6}; |
| 111 std::vector<size_t> expectedDeletions = {}; |
| 112 std::vector<size_t> expectedInsertions = {3, 4}; |
| 113 TestLevenshtein(initial, final, expectedSubstitutions, expectedDeletions, |
| 114 expectedInsertions); |
| 115 } |
| 116 |
| 117 TEST_F(TabSwitcherUtilsTest, |
| 118 TestLevenshteinPreferInsertionsAndDeletionsOverSubstitutions) { |
| 119 std::vector<size_t> initial = {0, 1}; |
| 120 std::vector<size_t> final = {1, 2}; |
| 121 std::vector<size_t> expectedSubstitutions = {}; |
| 122 std::vector<size_t> expectedDeletions = {0}; |
| 123 std::vector<size_t> expectedInsertions = {1}; |
| 124 TestLevenshtein(initial, final, expectedSubstitutions, expectedDeletions, |
| 125 expectedInsertions); |
| 126 } |
OLD | NEW |