|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2018 Christopher Friedt |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +#include <gtest/gtest.h> |
| 28 | + |
| 29 | +#include "util/ListNode.cpp" |
| 30 | + |
| 31 | +#include "linked-list-cycle-ii.cpp" |
| 32 | + |
| 33 | +TEST(LinkedListCycleIi, Test_3_2_0_n4__1) { |
| 34 | + vector<ListNode> nodes = { |
| 35 | + ListNode(3), |
| 36 | + ListNode(2), |
| 37 | + ListNode(0), |
| 38 | + ListNode(-4), |
| 39 | + }; |
| 40 | + nodes[0].next = &nodes[1]; |
| 41 | + nodes[1].next = &nodes[2]; |
| 42 | + nodes[2].next = &nodes[3]; |
| 43 | + nodes[3].next = &nodes[1]; |
| 44 | + EXPECT_EQ(&nodes[1], Solution().detectCycle(&nodes[0])); |
| 45 | +} |
| 46 | + |
| 47 | +TEST(LinkedListCycleIi, Test_1_2__0) { |
| 48 | + vector<ListNode> nodes = { |
| 49 | + ListNode(1), |
| 50 | + ListNode(2), |
| 51 | + }; |
| 52 | + nodes[0].next = &nodes[1]; |
| 53 | + nodes[1].next = &nodes[0]; |
| 54 | + EXPECT_EQ(&nodes[0], Solution().detectCycle(&nodes[0])); |
| 55 | +} |
| 56 | + |
| 57 | +TEST(LinkedListCycleIi, Test_1_n1) { |
| 58 | + vector<ListNode> nodes = { |
| 59 | + ListNode(1), |
| 60 | + }; |
| 61 | + nodes[0].val = 1; |
| 62 | + nodes[0].next = NULL; |
| 63 | + EXPECT_EQ(NULL, Solution().detectCycle(&nodes[0])); |
| 64 | +} |
0 commit comments