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