1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
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 
16 #include "tensorflow/core/public/version.h"
17 
18 #include <string>
19 #include "tensorflow/core/lib/core/stringpiece.h"
20 #include "tensorflow/core/lib/strings/str_util.h"
21 #include "tensorflow/core/platform/test.h"
22 
23 namespace tensorflow {
24 namespace {
25 
IsDotOrIdentifierChar(char c)26 bool IsDotOrIdentifierChar(char c) {
27   if (c == '.') return true;
28   if (c == '-') return true;
29   if (c >= 'A' && c <= 'Z') return true;
30   if (c >= 'a' && c <= 'z') return true;
31   if (c >= '0' && c <= '9') return true;
32   return false;
33 }
34 
ConsumeDotSeparatedIdentifiers(StringPiece * s,const string & prefix,StringPiece * val)35 bool ConsumeDotSeparatedIdentifiers(StringPiece* s, const string& prefix,
36                                     StringPiece* val) {
37   if (!str_util::ConsumePrefix(s, prefix)) return false;
38   size_t i;
39   for (i = 0; i < s->size() && IsDotOrIdentifierChar((*s)[i]); ++i) {
40     // Intentionally empty
41   }
42   *val = StringPiece(s->data(), i);
43   s->remove_prefix(i);
44   return i > 0;
45 }
46 
47 // Test that TF_VERSION_STRING follows semantic versioning.
TEST(SemverTest,VersionStringFollowsSemver)48 TEST(SemverTest, VersionStringFollowsSemver) {
49   // Poor approximation of the semver 2.0 specification at www.semver.org.  Feel
50   // free to refine further (for example, check for leading 0s in numbers), but
51   // avoid adding dependencies.
52   uint64 major, minor, patch;
53   StringPiece prerelease, metadata;
54   StringPiece semver(TF_VERSION_STRING);
55 
56   ASSERT_TRUE(str_util::ConsumeLeadingDigits(&semver, &major));
57   ASSERT_TRUE(str_util::ConsumePrefix(&semver, "."));
58   ASSERT_TRUE(str_util::ConsumeLeadingDigits(&semver, &minor));
59   ASSERT_TRUE(str_util::ConsumePrefix(&semver, "."));
60   // Till 0.11.0rc2, the prerelease version was (incorrectly) not separated from
61   // the patch version number. Let that slide.
62   // Remove this when TF_VERSION_STRING moves beyond 0.11.0rc2.
63   if (major == 0 && minor <= 11) {
64     return;
65   }
66   if (str_util::ConsumePrefix(&semver, "head")) {
67     ASSERT_TRUE(semver.empty());
68     return;
69   }
70   ASSERT_TRUE(str_util::ConsumeLeadingDigits(&semver, &patch));
71   if (semver.empty()) return;
72   if (semver[0] == '-') {
73     ASSERT_TRUE(ConsumeDotSeparatedIdentifiers(&semver, "-", &prerelease));
74   }
75   if (semver.empty()) return;
76   if (semver[0] == '+') {
77     ASSERT_TRUE(ConsumeDotSeparatedIdentifiers(&semver, "+", &metadata));
78   }
79   ASSERT_TRUE(semver.empty());
80 }
81 }  // namespace
82 }  // namespace tensorflow
83