blob: 58e3a4a6b31245e27a1b0147c4c94dd6b7ef7b47 [file] [log] [blame]
Andreas Wundsamf35ec812013-07-16 13:49:18 -07001#!/usr/bin/env python
2# Copyright 2013, Big Switch Networks, Inc.
3#
4# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
5# the following special exception:
6#
7# LOXI Exception
8#
9# As a special exception to the terms of the EPL, you may distribute libraries
10# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
11# that copyright and licensing notices generated by LoxiGen are not altered or removed
12# from the LoxiGen Libraries and the notice provided below is (i) included in
13# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
14# documentation for the LoxiGen Libraries, if distributed in binary form.
15#
16# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
17#
18# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
19# a copy of the EPL at:
20#
21# http://www.eclipse.org/legal/epl-v10.html
22#
23# Unless required by applicable law or agreed to in writing, software
24# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
26# EPL for the specific language governing permissions and limitations
27# under the EPL.
28
29import sys
30import os
31import unittest
32
33root_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..')
34sys.path.insert(0, root_dir)
35
36from generic_utils import *
37
38class MyHash(object):
39 def __init__(self, val):
40 self.val = val
41
42 def __hash__(self):
43 return hash(self.val)
44
45 def __str__(self):
46 return "BoringConstantString"
47
48 def __eq__(self, o ):
49 return type(self) == type(o) and self.val == o.val
50
51class GenericTest(unittest.TestCase):
52 def test_memoize_simple(self):
53 self.count = 0
54
55 @memoize
56 def function():
57 self.count += 1
58 return "Foo"
59
60 self.assertEquals(0, self.count)
61 self.assertEquals("Foo", function())
62 self.assertEquals(1, self.count)
63 self.assertEquals("Foo", function())
64 self.assertEquals(1, self.count)
65
66 def test_memoize_string_args(self):
67 self.count = 0
68
69 @memoize
70 def function(a, b):
71 self.count += 1
72 return "%s:%s" % (a,b)
73
74 self.assertEquals(0, self.count)
75 self.assertEquals("a:b", function('a', 'b'))
76 self.assertEquals(1, self.count)
77 self.assertEquals("ab:", function('ab', ''))
78 self.assertEquals(2, self.count)
79 self.assertEquals("ab:", function('ab', ''))
80 self.assertEquals(2, self.count)
81
82 def test_memoize_kw_args(self):
83 self.count = 0
84
85 @memoize
86 def function(**kw):
87 self.count += 1
88 return ",".join("{k}={v}".format(k=k,v=v) for k,v in kw.items())
89
90 self.assertEquals(0, self.count)
91 self.assertEquals("a=1", function(a=1))
92 self.assertEquals(1, self.count)
93 self.assertEquals("a=1,b=2", function(a=1, b=2))
94 self.assertEquals(2, self.count)
95 self.assertEquals("a=1", function(a=1))
96 self.assertEquals(2, self.count)
97 self.assertEquals("a=1,b=BoringConstantString", function(a=1, b=MyHash('1')))
98 self.assertEquals(3, self.count)
99
100 def test_memoize_with_hashable_object(self):
101 self.count = 0
102
103 @memoize
104 def function(a):
105 self.count += 1
106 return a.val
107
108 self.assertEquals(0, self.count)
109 self.assertEquals("a", function(MyHash('a')))
110 self.assertEquals(1, self.count)
111 self.assertEquals("b", function(MyHash('b')))
112 self.assertEquals(2, self.count)
113 self.assertEquals("a", function(MyHash('a')))
114 self.assertEquals(2, self.count)
115
116if __name__ == '__main__':
117 unittest.main()