xref: /aosp_15_r20/external/federated-compute/fcp/tensorflow/crc32_test.py (revision 14675a029014e728ec732f129a32e299b2da0601)
1*14675a02SAndroid Build Coastguard Worker# Copyright 2020 Google LLC
2*14675a02SAndroid Build Coastguard Worker#
3*14675a02SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*14675a02SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*14675a02SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*14675a02SAndroid Build Coastguard Worker#
7*14675a02SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
8*14675a02SAndroid Build Coastguard Worker#
9*14675a02SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*14675a02SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*14675a02SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*14675a02SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*14675a02SAndroid Build Coastguard Worker# limitations under the License.
14*14675a02SAndroid Build Coastguard Worker
15*14675a02SAndroid Build Coastguard Workerimport numpy as np
16*14675a02SAndroid Build Coastguard Workerimport tensorflow as tf
17*14675a02SAndroid Build Coastguard Workerfrom fcp.tensorflow import crc32
18*14675a02SAndroid Build Coastguard Worker
19*14675a02SAndroid Build Coastguard Worker
20*14675a02SAndroid Build Coastguard Workerclass CRC32Test(tf.test.TestCase):
21*14675a02SAndroid Build Coastguard Worker
22*14675a02SAndroid Build Coastguard Worker  def test_crc32(self):
23*14675a02SAndroid Build Coastguard Worker    assert 0 == crc32.crc32([])
24*14675a02SAndroid Build Coastguard Worker
25*14675a02SAndroid Build Coastguard Worker    # Constants taken from rfc3720 B.4
26*14675a02SAndroid Build Coastguard Worker    # 1. Tests on byte arrays
27*14675a02SAndroid Build Coastguard Worker    assert 0x8a9136aa == crc32.crc32(np.zeros(32, dtype=np.uint8))
28*14675a02SAndroid Build Coastguard Worker    assert 0x62a8ab43 == crc32.crc32(255 * np.ones(32, dtype=np.uint8))
29*14675a02SAndroid Build Coastguard Worker    x = np.arange(0, 0x20, dtype=np.uint8)
30*14675a02SAndroid Build Coastguard Worker    assert 0x46dd794e == crc32.crc32(x)
31*14675a02SAndroid Build Coastguard Worker    # 2. Tests for higher dimensional tensor shapes
32*14675a02SAndroid Build Coastguard Worker    assert 0x46dd794e == crc32.crc32(x.reshape(2, -1))
33*14675a02SAndroid Build Coastguard Worker    assert 0x46dd794e == crc32.crc32(x.reshape(4, 4, 2))
34*14675a02SAndroid Build Coastguard Worker    # Transpose will change memory order so checksum should change.
35*14675a02SAndroid Build Coastguard Worker    assert 0x46dd794e != crc32.crc32(x.reshape(2, -1).transpose())
36*14675a02SAndroid Build Coastguard Worker
37*14675a02SAndroid Build Coastguard Worker    # 3. Tests on int32, int64
38*14675a02SAndroid Build Coastguard Worker    assert crc32.crc32(tf.constant(0x123456789abcdef0,
39*14675a02SAndroid Build Coastguard Worker                                   dtype=tf.int64)) == crc32.crc32(
40*14675a02SAndroid Build Coastguard Worker                                       tf.constant([0x9abcdef0, 0x12345678],
41*14675a02SAndroid Build Coastguard Worker                                                   dtype=tf.int32))
42*14675a02SAndroid Build Coastguard Worker
43*14675a02SAndroid Build Coastguard Worker    # 4. IEEE float test. Not much to test here other than that the checksum
44*14675a02SAndroid Build Coastguard Worker    # produces the same result as it would on an integer tensor with the same
45*14675a02SAndroid Build Coastguard Worker    # memory representation.
46*14675a02SAndroid Build Coastguard Worker    assert crc32.crc32(tf.constant([-0.0, 0.0],
47*14675a02SAndroid Build Coastguard Worker                                   dtype=tf.float32)) == crc32.crc32(
48*14675a02SAndroid Build Coastguard Worker                                       tf.constant([0x80000000, 0x00000000],
49*14675a02SAndroid Build Coastguard Worker                                                   dtype=tf.int32))
50*14675a02SAndroid Build Coastguard Worker
51*14675a02SAndroid Build Coastguard Worker
52*14675a02SAndroid Build Coastguard Workerif __name__ == "__main__":
53*14675a02SAndroid Build Coastguard Worker  tf.test.main()
54