xref: /aosp_15_r20/external/rappor/bin/hash_candidates_test.py (revision 2abb31345f6c95944768b5222a9a5ed3fc68cc00)
1#!/usr/bin/python -S
2#
3# Copyright 2014 Google Inc. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""
18hash_candidates_test.py: Tests for hash_candidates.py
19"""
20
21import cStringIO
22import unittest
23
24import rappor
25import hash_candidates  # module under test
26
27
28STDIN = """\
29apple
30banana
31carrot
32"""
33
34EXPECTED_CSV_OUT = """\
35apple,5,1,26,26,38,34,63,62\r
36banana,12,14,28,24,37,34,62,49\r
37carrot,4,12,25,21,48,38,61,54\r
38"""
39
40
41class HashCandidatesTest(unittest.TestCase):
42
43  def setUp(self):
44    self.params = rappor.Params()
45    self.params.num_bloombits = 16
46    self.params.num_cohorts = 4
47    self.params.num_hashes = 2
48
49  def testHash(self):
50    stdin = cStringIO.StringIO(STDIN)
51    stdout = cStringIO.StringIO()
52
53    hash_candidates.HashCandidates(self.params, stdin, stdout)
54
55    self.assertMultiLineEqual(EXPECTED_CSV_OUT, stdout.getvalue())
56
57
58if __name__ == '__main__':
59  unittest.main()
60