1 // Copyright (c) 2015-2016 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <sstream>
16 #include <string>
17 
18 #include "gmock/gmock.h"
19 #include "test/unit_spirv.h"
20 
21 namespace spvtools {
22 namespace {
23 
24 using ::testing::AnyOf;
25 using ::testing::Eq;
26 using ::testing::Ge;
27 using ::testing::StartsWith;
28 
CheckFormOfHighLevelVersion(const std::string & version)29 void CheckFormOfHighLevelVersion(const std::string& version) {
30   std::istringstream s(version);
31   char v = 'x';
32   int year = -1;
33   char period = 'x';
34   int index = -1;
35   s >> v >> year >> period >> index;
36   EXPECT_THAT(v, Eq('v'));
37   EXPECT_THAT(year, Ge(2016));
38   EXPECT_THAT(period, Eq('.'));
39   EXPECT_THAT(index, Ge(0));
40   EXPECT_TRUE(s.good() || s.eof());
41 
42   std::string rest;
43   s >> rest;
44   EXPECT_THAT(rest, AnyOf("", "-dev"));
45 }
46 
TEST(SoftwareVersion,ShortIsCorrectForm)47 TEST(SoftwareVersion, ShortIsCorrectForm) {
48   SCOPED_TRACE("short form");
49   CheckFormOfHighLevelVersion(spvSoftwareVersionString());
50 }
51 
TEST(SoftwareVersion,DetailedIsCorrectForm)52 TEST(SoftwareVersion, DetailedIsCorrectForm) {
53   const std::string detailed_version(spvSoftwareVersionDetailsString());
54   EXPECT_THAT(detailed_version, StartsWith("SPIRV-Tools v"));
55 
56   // Parse the high level version.
57   const std::string from_v =
58       detailed_version.substr(detailed_version.find_first_of('v'));
59   const size_t first_space_after_v_or_npos = from_v.find_first_of(' ');
60   SCOPED_TRACE(detailed_version);
61   CheckFormOfHighLevelVersion(from_v.substr(0, first_space_after_v_or_npos));
62 
63   // We don't actually care about what comes after the version number.
64 }
65 
66 }  // namespace
67 }  // namespace spvtools
68