1#!/usr/bin/env python
2#
3# Public Domain 2014-2018 MongoDB, Inc.
4# Public Domain 2008-2014 WiredTiger, Inc.
5#
6# This is free and unencumbered software released into the public domain.
7#
8# Anyone is free to copy, modify, publish, use, compile, sell, or
9# distribute this software, either in source code form or as a compiled
10# binary, for any purpose, commercial or non-commercial, and by any
11# means.
12#
13# In jurisdictions that recognize copyright laws, the author or authors
14# of this software dedicate any and all copyright interest in the
15# software to the public domain. We make this dedication for the benefit
16# of the public at large and to the detriment of our heirs and
17# successors. We intend this dedication to be an overt act of
18# relinquishment in perpetuity of all present and future rights to this
19# software under copyright law.
20#
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27# OTHER DEALINGS IN THE SOFTWARE.
28#
29# test_pack.py
30#    Tests packing using public methods
31#
32
33import wiredtiger, wttest
34import re, sys
35
36class test_pack(wttest.WiredTigerTestCase):
37    name = 'test_pack'
38
39    def dump_cursor(self, cursor, name):
40        cursor.reset()
41        while cursor.next() == 0:
42            x = cursor.get_key()
43            y = cursor.get_value()
44            self.tty(' ' + name + ':  ' + str(x) + ' => ' + str(y))
45        cursor.reset()
46
47    def check(self, fmt, *v):
48        v = list(v)
49        fmtname = re.sub('([A-Z])', r'_\1', fmt)
50        uri = 'table:' + test_pack.name + '-' + fmtname
51        idx_uri = 'index:' + test_pack.name + '-' + fmtname + ':inverse'
52        nargs = len(v)
53        colnames = ",".join("v" + str(x) for x in xrange(nargs))
54        self.session.create(uri, "columns=(k," + colnames + ")," +
55                            "key_format=i,value_format=" + fmt)
56        self.session.create(idx_uri, "columns=(" + colnames + ")")
57        forw = self.session.open_cursor(uri, None, None)
58        forw_idx = self.session.open_cursor(idx_uri + "(k)", None, None)
59
60        forw.set_key(1234)
61        forw.set_value(*v)
62        forw.insert()
63
64        #self.dump_cursor(forw, 'forw')
65        #self.dump_cursor(forw_idx, 'index')
66
67        forw.set_key(1234)
68        self.assertEquals(forw.search(), 0)
69        got = forw.get_value()
70        if nargs == 1:  # API does not return a list, we want one for comparing
71            got = [got]
72        self.assertEquals(got, v)
73
74        forw_idx.set_key(*v)
75        self.assertEquals(forw_idx.search(), 0)
76        self.assertEquals(forw_idx.get_value(), 1234)
77        forw.close()
78        forw_idx.close()
79
80    def test_packing(self):
81        self.check('iii', 0, 101, -99)
82        self.check('3i', 0, 101, -99)
83        self.check('iS', 42, "forty two")
84
85        self.check('S', 'abc')
86        self.check('9S', 'a' * 9)
87        self.check('9SS', "forty two", "spam egg")
88        self.check('42S', 'a' * 42)
89        self.check('42SS', 'a' * 42, 'something')
90        self.check('S42S', 'something', 'a' * 42)
91        # nul terminated string with padding
92        self.check('10SS', 'aaaaa\x00\x00\x00\x00\x00', 'something')
93        self.check('S10S', 'something', 'aaaaa\x00\x00\x00\x00\x00')
94
95        self.check('u', r"\x42" * 20)
96        self.check('uu', r"\x42" * 10, r"\x42" * 10)
97        self.check('3u', r"\x4")
98        self.check('3uu', r"\x4", r"\x42" * 10)
99        self.check('u3u', r"\x42" * 10, r"\x4")
100        self.check('u', '\x00')
101        self.check('u', '')
102        self.check('uu', '', '\x00')
103        self.check('uu', '\x00', '')
104        self.check('uu', '', '')
105
106        self.check('s', "4")
107        self.check("1s", "4")
108        self.check("2s", "42")
109
110if __name__ == '__main__':
111    wttest.run()
112