CMake/Tests/CMakeLib/testFindPackageCommand.cxx
Pierluigi Taddei 31be918b0b find_package: Optionally sort globbed directories in a meaningful order
Add `CMAKE_FIND_PACKAGE_SORT_{ORDER,DIRECTION}` variables to specify
sort order and direction.

When multiple package with the same name have been found in the same
location sorting option can be used to force a specific version to be
loaded (e.g. libA_1.12.0 instead of libA_1.1.0).  Currently sorting by
NAME and by NATURAL order have been implemented.

Natural ordering makes use of the `strverscmp(3)` ordering.
2016-09-15 13:35:25 -04:00

77 lines
3.2 KiB
C++

/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2000-2011 Kitware, Inc., Insight Software Consortium
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#include "cmFindPackageCommand.h"
#include <iostream>
#include <string>
#define cmPassed(m) std::cout << "Passed: " << (m) << "\n"
#define cmFailed(m) \
std::cout << "FAILED: " << (m) << "\n"; \
failed = 1
int testFindPackageCommand(int /*unused*/, char* /*unused*/ [])
{
int failed = 0;
// ----------------------------------------------------------------------
// Test cmFindPackage::Sort
std::vector<std::string> testString;
testString.push_back("lib-0.0");
testString.push_back("lib-1.2");
testString.push_back("lib-2.0");
testString.push_back("lib-19.0.1");
testString.push_back("lib-20.01.1");
testString.push_back("lib-20.2.2a");
cmFindPackageCommand::Sort(testString.begin(), testString.end(),
cmFindPackageCommand::Natural,
cmFindPackageCommand::Asc);
if (!(testString[0] == "lib-0.0" && testString[1] == "lib-1.2" &&
testString[2] == "lib-2.0" && testString[3] == "lib-19.0.1" &&
testString[4] == "lib-20.01.1" && testString[5] == "lib-20.2.2a")) {
cmFailed("cmSystemTools::Sort fail with Natural ASC");
}
cmFindPackageCommand::Sort(testString.begin(), testString.end(),
cmFindPackageCommand::Natural,
cmFindPackageCommand::Dec);
if (!(testString[5] == "lib-0.0" && testString[4] == "lib-1.2" &&
testString[3] == "lib-2.0" && testString[2] == "lib-19.0.1" &&
testString[1] == "lib-20.01.1" && testString[0] == "lib-20.2.2a")) {
cmFailed("cmSystemTools::Sort fail with Natural ASC");
}
cmFindPackageCommand::Sort(testString.begin(), testString.end(),
cmFindPackageCommand::Name_order,
cmFindPackageCommand::Dec);
if (!(testString[5] == "lib-0.0" && testString[4] == "lib-1.2" &&
testString[3] == "lib-19.0.1" && testString[2] == "lib-2.0" &&
testString[1] == "lib-20.01.1" && testString[0] == "lib-20.2.2a")) {
cmFailed("cmSystemTools::Sort fail with Name DEC");
}
cmFindPackageCommand::Sort(testString.begin(), testString.end(),
cmFindPackageCommand::Name_order,
cmFindPackageCommand::Asc);
if (!(testString[0] == "lib-0.0" && testString[1] == "lib-1.2" &&
testString[2] == "lib-19.0.1" && testString[3] == "lib-2.0" &&
testString[4] == "lib-20.01.1" && testString[5] == "lib-20.2.2a")) {
cmFailed("cmSystemTools::Sort fail with Natural ASC");
}
if (!failed) {
cmPassed("cmSystemTools::Sort working");
}
return failed;
}