PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 12ms RepoModel.GetById 28ms app.codeStats 1ms

/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
 26import unittest
 27
 28from mergetools import merge, imerge
 29
 30class TestMerge(unittest.TestCase):
 31
 32    def testBasics(self):
 33        a = merge([1, 3, 5], [2, 4])
 34        self.assertEqual(a, [1, 2, 3, 4, 5])
 35        
 36        a = merge([1, 3, 5])
 37        self.assertEqual(a, [1, 3, 5])
 38    
 39
 40    def testEmpty(self):
 41        a = merge()
 42        self.assertEqual(a, [])
 43        
 44        a = merge([])
 45        self.assertEqual(a, [])
 46        
 47        a = merge([], [])
 48        self.assertEqual(a, [])
 49
 50        a = merge([], [], [])
 51        self.assertEqual(a, [])
 52
 53        a = merge([6], [])
 54        self.assertEqual(a, [6])
 55
 56        a = merge([], [6])
 57        self.assertEqual(a, [6])
 58
 59
 60    def testShort(self):
 61        a = merge([4], [1, 3, 5], [2, 6])
 62        self.assertEqual(a, [1, 2, 3, 4, 5, 6])
 63        
 64        a = merge([1, 3, 5], [4], [2, 6])
 65        self.assertEqual(a, [1, 2, 3, 4, 5, 6])
 66        
 67        a = merge([1, 3, 5], [2, 6], [4])
 68        self.assertEqual(a, [1, 2, 3, 4, 5, 6])
 69
 70
 71    def testMergeOverIters(self):
 72        a = merge(xrange(5, 21, 5), xrange(3, 21, 3), xrange(7, 21, 7))
 73        self.assertEqual(a, [3, 5, 6, 7, 9, 10, 12, 14, 15, 15, 18, 20])
 74
 75
 76class Thing(object):
 77    def __init__(self, name, value):
 78        self.name = name
 79        self.value = value
 80    
 81    def __lt__(self, other):
 82        return self.value < other.value
 83    
 84    def __str__(self):
 85        return self.name + "-" + str(self.value)
 86
 87
 88class TestOrdering(unittest.TestCase):
 89
 90    def testDuplicates(self):
 91        a = merge([Thing('amy', 3), Thing('bob', 3)],
 92                    [Thing('cy', 2), Thing('dawn', 4)])
 93        s = ','.join(map(str, a))
 94        self.assertEqual(s, 'cy-2,amy-3,bob-3,dawn-4')
 95        
 96        a = merge([Thing('amy', 3), Thing('bob', 3), Thing('harry', 5)], 
 97                    [Thing('cy', 2), Thing('guy', 3), Thing('dawn', 4)])
 98        s = ','.join(map(str, a))
 99        self.assertEqual(s, 'cy-2,amy-3,bob-3,guy-3,dawn-4,harry-5')
100        
101        
102    
103  
104if __name__ == '__main__':
105    unittest.main()