Commit bc856fd8 authored by Michael Büsch's avatar Michael Büsch

core: Add function to generate label names

Signed-off-by: Michael Büsch's avatarMichael Buesch <m@bues.ch>
parent ab9c3a5c
......@@ -82,3 +82,18 @@ class AwlLabel(object): #+cdef
if lbl.getLabelName() == label:
return i
return None
@classmethod
def generateLabelName(cls, index):
"""Generate a label name string.
The name does not include the final ':' character.
"""
maxIndex = (26 ** 4) - 1
if index < 0 or index > maxIndex:
raise ValueError("Label index %d out of range 0 - %d." % (
index, maxIndex))
labelChars = [None] * 4
for i in range(3, -1, -1):
labelChars[i] = chr(ord("A") + (index % 26))
index //= 26
return "".join(labelChars)
from __future__ import division, absolute_import, print_function, unicode_literals
from awlsim_tstlib import *
from awlsim.core.labels import *
class Test_AwlLabels(object):
def test_generateLabelName(self):
assert_raises(ValueError, lambda: AwlLabel.generateLabelName(-1))
assert_eq(AwlLabel.generateLabelName(0), "AAAA")
assert_eq(AwlLabel.generateLabelName(26**1 - 1), "AAAZ")
assert_eq(AwlLabel.generateLabelName(26**1), "AABA")
assert_eq(AwlLabel.generateLabelName(26**2 - 1), "AAZZ")
assert_eq(AwlLabel.generateLabelName(26**2), "ABAA")
assert_eq(AwlLabel.generateLabelName(26**3 - 1), "AZZZ")
assert_eq(AwlLabel.generateLabelName(26**3), "BAAA")
assert_eq(AwlLabel.generateLabelName(26**4 - 1), "ZZZZ")
assert_raises(ValueError, lambda: AwlLabel.generateLabelName(26**4))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment