xref: /aosp_15_r20/external/libgav1/src/version_test.cc (revision 095378508e87ed692bf8dfeb34008b65b3735891)
1 // Copyright 2021 The libgav1 Authors
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 "src/gav1/version.h"
16 
17 #include <regex>  // NOLINT (unapproved c++11 header)
18 
19 #include "gtest/gtest.h"
20 
21 namespace libgav1 {
22 namespace {
23 
TEST(VersionTest,GetVersion)24 TEST(VersionTest, GetVersion) {
25   const int library_version = GetVersion();
26   EXPECT_EQ((library_version >> 24) & 0xff, 0);
27   // Note if we link against a shared object there's potential for a mismatch
28   // if a different library is loaded at runtime.
29   EXPECT_EQ((library_version >> 16) & 0xff, LIBGAV1_MAJOR_VERSION);
30   EXPECT_EQ((library_version >> 8) & 0xff, LIBGAV1_MINOR_VERSION);
31   EXPECT_EQ(library_version & 0xff, LIBGAV1_PATCH_VERSION);
32 
33   const int header_version = LIBGAV1_VERSION;
34   EXPECT_EQ((header_version >> 24) & 0xff, 0);
35   EXPECT_EQ((header_version >> 16) & 0xff, LIBGAV1_MAJOR_VERSION);
36   EXPECT_EQ((header_version >> 8) & 0xff, LIBGAV1_MINOR_VERSION);
37   EXPECT_EQ(header_version & 0xff, LIBGAV1_PATCH_VERSION);
38 }
39 
TEST(VersionTest,GetVersionString)40 TEST(VersionTest, GetVersionString) {
41   const char* version = GetVersionString();
42   ASSERT_NE(version, nullptr);
43   // https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
44   const std::regex semver_regex(
45       R"(^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*))"
46       R"((?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))"
47       R"((?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?)"
48       R"((?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$)");
49 
50   EXPECT_TRUE(std::regex_match(version, semver_regex)) << version;
51   // Regex validation:
52   // It shouldn't accept a version starting with a non-digit.
53   version = "v1.2.3";
54   EXPECT_FALSE(std::regex_match(version, semver_regex)) << version;
55   // It shouldn't accept a version with spaces."
56   version = "1.2.3 alpha";
57   EXPECT_FALSE(std::regex_match(version, semver_regex)) << version;
58 }
59 
TEST(VersionTest,GetBuildConfiguration)60 TEST(VersionTest, GetBuildConfiguration) {
61   const char* config = GetBuildConfiguration();
62   ASSERT_NE(config, nullptr);
63 }
64 
65 }  // namespace
66 }  // namespace libgav1
67