PageRenderTime 21ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/historical/mergetools_test.py

https://bitbucket.org/lindenlab/apiary/
Python | 105 lines | 51 code | 30 blank | 24 comment | 1 complexity | 58efc00bc26c90219e4082dcbc415a30 MD5 | raw file
  1. #
  2. # $LicenseInfo:firstyear=2010&license=mit$
  3. #
  4. # Copyright (c) 2010, Linden Research, Inc.
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. # THE SOFTWARE.
  23. # $/LicenseInfo$
  24. #
  25. import unittest
  26. from mergetools import merge, imerge
  27. class TestMerge(unittest.TestCase):
  28. def testBasics(self):
  29. a = merge([1, 3, 5], [2, 4])
  30. self.assertEqual(a, [1, 2, 3, 4, 5])
  31. a = merge([1, 3, 5])
  32. self.assertEqual(a, [1, 3, 5])
  33. def testEmpty(self):
  34. a = merge()
  35. self.assertEqual(a, [])
  36. a = merge([])
  37. self.assertEqual(a, [])
  38. a = merge([], [])
  39. self.assertEqual(a, [])
  40. a = merge([], [], [])
  41. self.assertEqual(a, [])
  42. a = merge([6], [])
  43. self.assertEqual(a, [6])
  44. a = merge([], [6])
  45. self.assertEqual(a, [6])
  46. def testShort(self):
  47. a = merge([4], [1, 3, 5], [2, 6])
  48. self.assertEqual(a, [1, 2, 3, 4, 5, 6])
  49. a = merge([1, 3, 5], [4], [2, 6])
  50. self.assertEqual(a, [1, 2, 3, 4, 5, 6])
  51. a = merge([1, 3, 5], [2, 6], [4])
  52. self.assertEqual(a, [1, 2, 3, 4, 5, 6])
  53. def testMergeOverIters(self):
  54. a = merge(xrange(5, 21, 5), xrange(3, 21, 3), xrange(7, 21, 7))
  55. self.assertEqual(a, [3, 5, 6, 7, 9, 10, 12, 14, 15, 15, 18, 20])
  56. class Thing(object):
  57. def __init__(self, name, value):
  58. self.name = name
  59. self.value = value
  60. def __lt__(self, other):
  61. return self.value < other.value
  62. def __str__(self):
  63. return self.name + "-" + str(self.value)
  64. class TestOrdering(unittest.TestCase):
  65. def testDuplicates(self):
  66. a = merge([Thing('amy', 3), Thing('bob', 3)],
  67. [Thing('cy', 2), Thing('dawn', 4)])
  68. s = ','.join(map(str, a))
  69. self.assertEqual(s, 'cy-2,amy-3,bob-3,dawn-4')
  70. a = merge([Thing('amy', 3), Thing('bob', 3), Thing('harry', 5)],
  71. [Thing('cy', 2), Thing('guy', 3), Thing('dawn', 4)])
  72. s = ','.join(map(str, a))
  73. self.assertEqual(s, 'cy-2,amy-3,bob-3,guy-3,dawn-4,harry-5')
  74. if __name__ == '__main__':
  75. unittest.main()