aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/util/semver_test.cc
blob: fdc34fa58bdebf529e3c9b2771b274e5fe6f6d50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/core/public/version.h"

#include <string>
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/strings/str_util.h"
#include "tensorflow/core/platform/test.h"

namespace tensorflow {
namespace {

bool IsDotOrIdentifierChar(char c) {
  if (c == '.') return true;
  if (c == '-') return true;
  if (c >= 'A' && c <= 'Z') return true;
  if (c >= 'a' && c <= 'z') return true;
  if (c >= '0' && c <= '9') return true;
  return false;
}

bool ConsumeDotSeparatedIdentifiers(StringPiece* s, const string& prefix,
                                    StringPiece* val) {
  if (!str_util::ConsumePrefix(s, prefix)) return false;
  size_t i;
  for (i = 0; i < s->size() && IsDotOrIdentifierChar((*s)[i]); ++i) {
    // Intentionally empty
  }
  *val = StringPiece(s->data(), i);
  s->remove_prefix(i);
  return i > 0;
}

// Test that TF_VERSION_STRING follows semantic versioning.
TEST(SemverTest, VersionStringFollowsSemver) {
  // Poor approximation of the semver 2.0 specification at www.semver.org.  Feel
  // free to refine further (for example, check for leading 0s in numbers), but
  // avoid adding dependencies.
  uint64 major, minor, patch;
  StringPiece prerelease, metadata;
  StringPiece semver(TF_VERSION_STRING);

  ASSERT_TRUE(str_util::ConsumeLeadingDigits(&semver, &major));
  ASSERT_TRUE(str_util::ConsumePrefix(&semver, "."));
  ASSERT_TRUE(str_util::ConsumeLeadingDigits(&semver, &minor));
  ASSERT_TRUE(str_util::ConsumePrefix(&semver, "."));
  // Till 0.11.0rc2, the prerelease version was (incorrectly) not separated from
  // the patch version number. Let that slide.
  // Remove this when TF_VERSION_STRING moves beyond 0.11.0rc2.
  if (major == 0 && minor <= 11) {
    return;
  }
  if (str_util::ConsumePrefix(&semver, "head")) {
    ASSERT_TRUE(semver.empty());
    return;
  }
  ASSERT_TRUE(str_util::ConsumeLeadingDigits(&semver, &patch));
  if (semver.empty()) return;
  if (semver[0] == '-') {
    ASSERT_TRUE(ConsumeDotSeparatedIdentifiers(&semver, "-", &prerelease));
  }
  if (semver.empty()) return;
  if (semver[0] == '+') {
    ASSERT_TRUE(ConsumeDotSeparatedIdentifiers(&semver, "+", &metadata));
  }
  ASSERT_TRUE(semver.empty());
}
}  // namespace
}  // namespace tensorflow