inifile.test.cc revision 13465
113465Sgabeblack@google.com/* 213465Sgabeblack@google.com * Copyright (c) 2018 ARM Limited 313465Sgabeblack@google.com * All rights reserved 413465Sgabeblack@google.com * 513465Sgabeblack@google.com * Copyright (c) 2002-2005 The Regents of The University of Michigan 613465Sgabeblack@google.com * All rights reserved. 713465Sgabeblack@google.com * 813465Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 913465Sgabeblack@google.com * modification, are permitted provided that the following conditions are 1013465Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 1113465Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 1213465Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 1313465Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1413465Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1513465Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1613465Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1713465Sgabeblack@google.com * this software without specific prior written permission. 1813465Sgabeblack@google.com * 1913465Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2013465Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2113465Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2213465Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2313465Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2413465Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2513465Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2613465Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2713465Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2813465Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2913465Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3013465Sgabeblack@google.com * 3113465Sgabeblack@google.com * Authors: Nathan Binkert 3213465Sgabeblack@google.com * Steve Reinhardt 3313465Sgabeblack@google.com */ 3413465Sgabeblack@google.com 3513465Sgabeblack@google.com#include <gtest/gtest.h> 3613465Sgabeblack@google.com 3713465Sgabeblack@google.com#include <fstream> 3813465Sgabeblack@google.com#include <iostream> 3913465Sgabeblack@google.com#include <string> 4013465Sgabeblack@google.com#include <vector> 4113465Sgabeblack@google.com 4213465Sgabeblack@google.com#include "base/inifile.hh" 4313465Sgabeblack@google.com 4413465Sgabeblack@google.comusing namespace std; 4513465Sgabeblack@google.com 4613465Sgabeblack@google.comnamespace { 4713465Sgabeblack@google.com 4813465Sgabeblack@google.comstd::istringstream iniFile(R"ini_file( 4913465Sgabeblack@google.com[General] 5013465Sgabeblack@google.com Test1=BARasdf 5113465Sgabeblack@google.com Test2=bar 5213465Sgabeblack@google.com 5313465Sgabeblack@google.com[Junk] 5413465Sgabeblack@google.comTest3=yo 5513465Sgabeblack@google.comTest4=mama 5613465Sgabeblack@google.com 5713465Sgabeblack@google.com[Foo] 5813465Sgabeblack@google.comFoo1=89 5913465Sgabeblack@google.comFoo2=384 6013465Sgabeblack@google.com 6113465Sgabeblack@google.com[General] 6213465Sgabeblack@google.comTest3=89 6313465Sgabeblack@google.com 6413465Sgabeblack@google.com[Junk] 6513465Sgabeblack@google.comTest4+=mia 6613465Sgabeblack@google.com)ini_file"); 6713465Sgabeblack@google.com 6813465Sgabeblack@google.com}; 6913465Sgabeblack@google.com 7013465Sgabeblack@google.comTEST(Initest, MatchFound) 7113465Sgabeblack@google.com{ 7213465Sgabeblack@google.com IniFile simConfigDB; 7313465Sgabeblack@google.com simConfigDB.load(iniFile); 7413465Sgabeblack@google.com 7513465Sgabeblack@google.com std::string value; 7613465Sgabeblack@google.com 7713465Sgabeblack@google.com auto ret = simConfigDB.find("General", "Test2", value); 7813465Sgabeblack@google.com ASSERT_TRUE(ret); 7913465Sgabeblack@google.com ASSERT_STREQ(value.c_str(), "bar"); 8013465Sgabeblack@google.com 8113465Sgabeblack@google.com ret = simConfigDB.find("Junk", "Test3", value); 8213465Sgabeblack@google.com ASSERT_TRUE(ret); 8313465Sgabeblack@google.com ASSERT_STREQ(value.c_str(), "yo"); 8413465Sgabeblack@google.com 8513465Sgabeblack@google.com ret = simConfigDB.find("Junk", "Test4", value); 8613465Sgabeblack@google.com ASSERT_TRUE(ret); 8713465Sgabeblack@google.com ASSERT_STREQ(value.c_str(), "mama mia"); 8813465Sgabeblack@google.com 8913465Sgabeblack@google.com ret = simConfigDB.find("General", "Test1", value); 9013465Sgabeblack@google.com ASSERT_TRUE(ret); 9113465Sgabeblack@google.com ASSERT_STREQ(value.c_str(), "BARasdf"); 9213465Sgabeblack@google.com 9313465Sgabeblack@google.com ret = simConfigDB.find("General", "Test3", value); 9413465Sgabeblack@google.com ASSERT_TRUE(ret); 9513465Sgabeblack@google.com ASSERT_STREQ(value.c_str(), "89"); 9613465Sgabeblack@google.com} 9713465Sgabeblack@google.com 9813465Sgabeblack@google.comTEST(Initest, MatchNotFound) 9913465Sgabeblack@google.com{ 10013465Sgabeblack@google.com IniFile simConfigDB; 10113465Sgabeblack@google.com simConfigDB.load(iniFile); 10213465Sgabeblack@google.com 10313465Sgabeblack@google.com std::string value; 10413465Sgabeblack@google.com 10513465Sgabeblack@google.com auto ret = simConfigDB.find("Junk2", "test3", value); 10613465Sgabeblack@google.com ASSERT_FALSE(ret); 10713465Sgabeblack@google.com 10813465Sgabeblack@google.com ret = simConfigDB.find("Junk", "test4", value); 10913465Sgabeblack@google.com ASSERT_FALSE(ret); 11013465Sgabeblack@google.com} 111