Best Python code snippet using tempest_python
test_states.py
Source:test_states.py
1"""2 Beta Expansions of Salem Numbers, calculating periods thereof3 Copyright (C) 2021 Michael P. Lane4 This program is free software: you can redistribute it and/or modify5 it under the terms of the GNU General Public License as published by6 the Free Software Foundation, either version 3 of the License, or7 (at your option) any later version.8 This program is distributed in the hope that it will be useful,9 but WITHOUT ANY WARRANTY; without even the implied warranty of10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11 GNU General Public License for more details.12"""13import copy14from unittest import TestCase15import numpy as np16from advanced.utilities import set_up_save_states, iter_over_all, iter_over_completes, iter_over_incompletes17from beta_numbers.data.states import Save_State_Type, Disk_Data18from beta_numbers.utilities import Int_Polynomial19from beta_numbers.utilities.periodic_lists import calc_beginning_index_of_redundant_data20class Test_Disk_Data(TestCase):21 def setUp(self):22 set_up_save_states(self)23 def test___init__(self):24 with self.subTest():25 with self.assertRaises(ValueError):26 Disk_Data(Save_State_Type.CS, self.beta, ["hi"], -1)27 def test_get_beta(self):28 for save_state in iter_over_all(self):29 with self.subTest():30 self.assertEqual(self.beta, save_state.get_beta())31 def test_mark_complete(self):32 for save_state in iter_over_completes(self):33 with self.subTest():34 self.assertTrue(save_state.is_complete)35 with self.subTest():36 self.assertEqual(self.p, save_state.p)37 with self.subTest():38 self.assertEqual(self.m, save_state.m)39 for save_state in iter_over_incompletes(self):40 with self.subTest():41 self.assertFalse(save_state.is_complete)42 with self.subTest():43 self.assertIsNone(save_state.p)44 with self.subTest():45 self.assertIsNone(save_state.m)46 def test_get_metadata(self):47 for save_state in iter_over_all(self):48 metadata = save_state.get_metadata()49 with self.subTest():50 self.assertTrue(51 metadata.type == save_state.type and52 metadata.get_beta() == save_state.get_beta() and53 metadata.start_n == save_state.start_n and54 metadata.data is None and55 len(metadata) == len(save_state) and56 metadata.is_complete == save_state.is_complete and57 metadata.p == save_state.p and58 metadata.m == save_state.m,59 str((metadata, save_state))60 )61 def test_length(self):62 for save_state in iter_over_all(self):63 with self.subTest():64 self.assertEqual(len(save_state.data), len(save_state))65 with self.subTest():66 self.assertEqual(len(save_state), len(save_state.get_metadata()))67 def test___eq__(self):68 for save_state in iter_over_all(self):69 save_state1 = copy.copy(save_state)70 save_state2 = copy.copy(save_state)71 with self.subTest():72 self.assertEqual(save_state1, save_state2)73 save_state1.beta0 = 42074 save_state2.beta0 = 6975 with self.subTest():76 self.assertEqual(save_state1,save_state2)77 save_state1 = copy.copy(save_state)78 save_state2 = copy.copy(save_state)79 save_state1.type = Save_State_Type.CS80 save_state2.type = Save_State_Type.BS81 with self.subTest():82 self.assertNotEqual(save_state1,save_state2, "types should be different")83 save_state1 = copy.copy(save_state)84 save_state2 = copy.copy(save_state)85 save_state1.beta.dps = 3286 save_state2.beta = copy.copy(save_state1.beta)87 save_state2.beta.dps = 6488 with self.subTest():89 self.assertNotEqual(save_state1, save_state2, "dps should be different")90 save_state1 = copy.copy(save_state)91 save_state2 = copy.copy(save_state)92 save_state1.start_n = 193 save_state2.start_n = 294 with self.subTest():95 self.assertNotEqual(save_state1, save_state2, "start_n should be different")96 save_state1 = copy.copy(save_state)97 save_state2 = copy.copy(save_state)98 save_state1.data = [1]99 save_state2.data = [2]100 with self.subTest():101 self.assertEqual(save_state1, save_state2, "data should not make a difference")102 save_state1 = copy.copy(save_state)103 save_state2 = copy.copy(save_state)104 save_state1.is_complete = True105 save_state2.is_complete = False106 with self.subTest():107 self.assertNotEqual(save_state1, save_state2, "is_complete should be different")108 save_state1 = copy.copy(save_state)109 save_state2 = copy.copy(save_state)110 save_state1._length = 1111 save_state2._length = 2112 with self.subTest():113 self.assertNotEqual(save_state1, save_state2, "length should be different")114 save_state1 = copy.copy(save_state)115 save_state2 = copy.copy(save_state)116 save_state1.p = 1117 save_state2.p = 2118 with self.subTest():119 self.assertNotEqual(save_state1, save_state2, "p should be different")120 save_state1 = copy.copy(save_state)121 save_state2 = copy.copy(save_state)122 save_state1.m = 1123 save_state2.m = 2124 with self.subTest():125 self.assertNotEqual(save_state1, save_state2, "m should be different")126 def test___hash__(self):127 for save_state in iter_over_all(self):128 save_state1 = copy.copy(save_state)129 save_state2 = copy.copy(save_state)130 with self.subTest():131 self.assertEqual(hash(save_state1), hash(save_state2))132 save_state1.beta0 = 420133 save_state2.beta0 = 69134 with self.subTest():135 self.assertEqual(hash(save_state1),hash(save_state2))136 save_state1 = copy.copy(save_state)137 save_state2 = copy.copy(save_state)138 save_state1.type = Save_State_Type.CS139 save_state2.type = Save_State_Type.BS140 with self.subTest():141 self.assertNotEqual(hash(save_state1),hash(save_state2), "types should be different")142 save_state1 = copy.copy(save_state)143 save_state2 = copy.copy(save_state)144 save_state1.start_n = 1145 save_state2.start_n = 2146 with self.subTest():147 self.assertNotEqual(hash(save_state1), hash(save_state2), "start_n should be different")148 save_state1 = copy.copy(save_state)149 save_state2 = copy.copy(save_state)150 save_state1.is_complete = True151 save_state2.is_complete = False152 with self.subTest():153 self.assertNotEqual(hash(save_state1), hash(save_state2), "is_complete should be different")154 save_state1 = copy.copy(save_state)155 save_state2 = copy.copy(save_state)156 save_state1._length = 1157 save_state2._length = 2158 with self.subTest():159 self.assertNotEqual(hash(save_state1), hash(save_state2), "length should be different")160 save_state1 = copy.copy(save_state)161 save_state2 = copy.copy(save_state)162 save_state1.p = 1163 save_state2.p = 2164 with self.subTest():165 self.assertNotEqual(hash(save_state1), hash(save_state2), "p should be different")166 save_state1 = copy.copy(save_state)167 save_state2 = copy.copy(save_state)168 save_state1.m = 1169 save_state2.m = 2170 with self.subTest():171 self.assertNotEqual(hash(save_state1), hash(save_state2), "m should be different")172 def test___contains__(self):173 for save_state in iter_over_all(self):174 with self.subTest():175 self.assertNotIn(-1, save_state)176 with self.subTest():177 self.assertNotIn(save_state.start_n-1, save_state)178 with self.subTest():179 self.assertIn(save_state.start_n, save_state)180 with self.subTest():181 self.assertIn(save_state.start_n + len(save_state) - 1, save_state)182 with self.subTest():183 self.assertNotIn(save_state.start_n + len(save_state), save_state)184 def test___getitem__(self):185 for save_state in iter_over_all(self):186 with self.subTest():187 with self.assertRaises(IndexError):188 save_state[-1]189 with self.subTest():190 with self.assertRaises(IndexError):191 save_state[save_state.start_n-1]192 with self.subTest():193 with self.assertRaises(IndexError):194 save_state[save_state.start_n+len(save_state)]195 for i in range(len(save_state)):196 with self.subTest():197 self.assertEqual(save_state[save_state.start_n+i], save_state.data[i])198 def test_get_slice(self):199 for save_state in iter_over_all(self):200 with self.subTest():201 with self.assertRaises(IndexError):202 save_state.get_slice(-1, save_state.start_n+1)203 with self.subTest():204 with self.assertRaises(IndexError):205 save_state.get_slice(save_state.start_n, save_state.start_n + len(save_state)+1)206 if len(save_state) > 1:207 _save_state = Disk_Data(save_state.type,self.beta, save_state.data[1:], save_state.start_n+1)208 if save_state.is_complete:209 _save_state.mark_complete(save_state.p,save_state.m)210 with self.subTest():211 self.assertEqual(212 save_state.get_slice(save_state.start_n+1, save_state.start_n + len(save_state)),213 _save_state,214 "slice off beginning error"215 )216 _save_state = Disk_Data(save_state.type,self.beta, save_state.data[:-1], save_state.start_n)217 if save_state.is_complete:218 _save_state.mark_complete(save_state.p,save_state.m)219 with self.subTest():220 self.assertEqual(221 save_state.get_slice(save_state.start_n, save_state.start_n + len(save_state) - 1),222 _save_state,223 "slice off end error"224 )225 if len(save_state) > 2:226 _save_state = Disk_Data(save_state.type,self.beta, save_state.data[1:-1], save_state.start_n+1)227 if save_state.is_complete:228 _save_state.mark_complete(save_state.p, save_state.m)229 with self.subTest():230 self.assertEqual(231 save_state.get_slice(save_state.start_n+1, save_state.start_n + len(save_state) - 1),232 _save_state,233 "slice off beginning and end error"234 )235 # for save_state in self.iter_over_completes():236 # if len(save_state) > 1:237 # self.assertFalse(save_state.get_slice(save_state.start_n+1, save_state.start_n + len(save_state)).is_complete)238 def test_remove_redundancies(self):239 for save_state in iter_over_incompletes(self):240 data = copy.deepcopy(save_state.data)241 save_state.remove_redundancies()242 self.assertTrue(np.all(data == save_state.data), "inequal incomplete data")243 for save_state in iter_over_completes(self):244 data = copy.deepcopy(save_state.data)245 _save_state = copy.copy(save_state)246 _save_state.remove_redundancies()247 self.assertTrue(248 np.all(249 data[:calc_beginning_index_of_redundant_data(save_state.start_n, save_state.p, save_state.m)] ==250 _save_state.data251 ),252 "inequal chopped data"253 )254class Test_Ram_Data(TestCase):255 def test_append(self):pass256 def test_trim_initial(self): pass257 def test_set_start_n(self): pass258 def test_clear(self): pass259 def test_cast_to_save_stae(self): pass...
python_state_test.py
Source:python_state_test.py
1# Copyright 2018 The TensorFlow Authors. All Rights Reserved.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 at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# 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 and13# limitations under the License.14# ==============================================================================15from __future__ import absolute_import16from __future__ import division17from __future__ import print_function18import os19import numpy20from tensorflow.contrib.checkpoint.python import python_state21from tensorflow.python.client import session22from tensorflow.python.eager import test23from tensorflow.python.framework import ops24from tensorflow.python.framework import test_util25from tensorflow.python.ops import variables26from tensorflow.python.training.checkpointable import util27class NumpyStateTests(test.TestCase):28 @test_util.run_in_graph_and_eager_modes29 def testSaveRestoreNumpyState(self):30 directory = self.get_temp_dir()31 prefix = os.path.join(directory, "ckpt")32 save_state = python_state.NumpyState()33 saver = util.Checkpoint(numpy=save_state)34 save_state.a = numpy.ones([2, 2])35 save_state.b = numpy.ones([2, 2])36 save_state.b = numpy.zeros([2, 2])37 save_state.c = numpy.int64(3)38 self.assertAllEqual(numpy.ones([2, 2]), save_state.a)39 self.assertAllEqual(numpy.zeros([2, 2]), save_state.b)40 self.assertEqual(3, save_state.c)41 first_save_path = saver.save(prefix)42 save_state.a[1, 1] = 2.43 save_state.c = numpy.int64(4)44 second_save_path = saver.save(prefix)45 load_state = python_state.NumpyState()46 loader = util.Checkpoint(numpy=load_state)47 loader.restore(first_save_path).initialize_or_restore()48 self.assertAllEqual(numpy.ones([2, 2]), load_state.a)49 self.assertAllEqual(numpy.zeros([2, 2]), load_state.b)50 self.assertEqual(3, load_state.c)51 load_state.a[0, 0] = 42.52 self.assertAllEqual([[42., 1.], [1., 1.]], load_state.a)53 loader.restore(first_save_path).run_restore_ops()54 self.assertAllEqual(numpy.ones([2, 2]), load_state.a)55 loader.restore(second_save_path).run_restore_ops()56 self.assertAllEqual([[1., 1.], [1., 2.]], load_state.a)57 self.assertAllEqual(numpy.zeros([2, 2]), load_state.b)58 self.assertEqual(4, load_state.c)59 def testNoGraphPollution(self):60 graph = ops.Graph()61 with graph.as_default(), session.Session():62 directory = self.get_temp_dir()63 prefix = os.path.join(directory, "ckpt")64 save_state = python_state.NumpyState()65 saver = util.Checkpoint(numpy=save_state)66 save_state.a = numpy.ones([2, 2])67 save_path = saver.save(prefix)68 saver.restore(save_path)69 graph.finalize()70 saver.save(prefix)71 save_state.a = numpy.zeros([2, 2])72 saver.save(prefix)73 saver.restore(save_path)74 @test_util.run_in_graph_and_eager_modes75 def testNoMixedNumpyStateTF(self):76 save_state = python_state.NumpyState()77 save_state.a = numpy.ones([2, 2])78 with self.assertRaises(NotImplementedError):79 save_state.v = variables.Variable(1.)80 @test_util.run_in_graph_and_eager_modes81 def testDocstringExample(self):82 arrays = python_state.NumpyState()83 checkpoint = util.Checkpoint(numpy_arrays=arrays)84 arrays.x = numpy.zeros([3, 4])85 save_path = checkpoint.save(os.path.join(self.get_temp_dir(), "ckpt"))86 arrays.x[1, 1] = 4.87 checkpoint.restore(save_path)88 self.assertAllEqual(numpy.zeros([3, 4]), arrays.x)89 second_checkpoint = util.Checkpoint(numpy_arrays=python_state.NumpyState())90 second_checkpoint.restore(save_path)91 self.assertAllEqual(numpy.zeros([3, 4]), second_checkpoint.numpy_arrays.x)92if __name__ == "__main__":...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!