| 1 | /* |
| 2 | * Copyright (C) 2016 Edward d'Auvergne |
| 3 | * |
| 4 | * This file is part of the program FlightGear. |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | |
| 21 | #include "test_props.hxx" |
| 22 | |
| 23 | #include <simgear/props/props_io.hxx> |
| 24 | |
| 25 | // Set up function for each test. |
| 26 | void SimgearPropsTests::setUp() |
| 27 | { |
| 28 | // Create a property tree. |
| 29 | tree = new SGPropertyNode; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | // Clean up after each test. |
| 34 | void SimgearPropsTests::tearDown() |
| 35 | { |
| 36 | // Delete the tree (avoiding the memory leak). |
| 37 | delete tree; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | // Test property aliasing, to catch possible memory leaks. |
| 42 | void SimgearPropsTests::testAliasLeak() |
| 43 | { |
| 44 | // Declarations. |
| 45 | SGPropertyNode *alias; |
| 46 | |
| 47 | // Create a new node. |
| 48 | tree->getNode("test-node", true); |
| 49 | |
| 50 | // Aliased node. |
| 51 | alias = tree->getNode("test-alias", true); |
| 52 | alias->alias("test-node", false); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | void SimgearPropsTests::testDoubleAlias() |
| 57 | { |
| 58 | // Declarations. |
| 59 | SGPropertyNode* alias; |
| 60 | |
| 61 | // Create a new node. |
| 62 | tree->getNode("test-node", true); |
| 63 | tree->getNode("another-node", true); |
| 64 | |
| 65 | // Aliased node. |
| 66 | alias = tree->getNode("test-alias", true); |
| 67 | |
| 68 | CPPUNIT_ASSERT(alias->alias("test-node", false)); |
| 69 | CPPUNIT_ASSERT(!alias->alias("another-node", false)); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | void SimgearPropsTests::testPropsCopyIf() |
| 74 | { |
| 75 | // dummy property tree structure |
| 76 | tree->setIntValue("a/a/a", 42); |
| 77 | tree->setStringValue("a/a/b", "foo"); |
| 78 | tree->setIntValue("a/a/c[2]", 99); |
| 79 | tree->setIntValue("a/a/d", 1); |
| 80 | tree->setIntValue("a/b/a[0]", 50); |
| 81 | tree->setIntValue("a/b/a[1]", 100); |
| 82 | tree->setStringValue("a/b/b", "foo"); |
| 83 | |
| 84 | SGPropertyNode_ptr destA(new SGPropertyNode); |
| 85 | |
| 86 | copyPropertiesIf(tree, destA, [](const SGPropertyNode* src) { |
| 87 | // always copy non-leaf nodes |
| 88 | if (src->nChildren() > 0) |
| 89 | return true; |
| 90 | |
| 91 | return (src->getType() == simgear::props::INT) && |
| 92 | src->getIntValue() > 50; |
| 93 | }); |
| 94 | |
| 95 | CPPUNIT_ASSERT_EQUAL(1, destA->getNode("a/a")->nChildren()); // only 99 |
| 96 | CPPUNIT_ASSERT_EQUAL(99, destA->getIntValue("a/a/c[2]")); |
| 97 | CPPUNIT_ASSERT_EQUAL(1, destA->getNode("a/b")->nChildren()); // only 100 |
| 98 | CPPUNIT_ASSERT_EQUAL(100, destA->getIntValue("a/b/a[1]")); |
| 99 | } |