




"""simple docstring"""





import unittest

import numpy as np

from transformers.testing_utils import is_flaky, require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import DonutImageProcessor



class   a__					(						unittest.TestCase ):


			def __init__(   self   :     Union[str, Any]   ,	UpperCamelCase_   :     Dict   ,	UpperCamelCase_   :     int=7   ,	UpperCamelCase_   :     Dict=3   ,	UpperCamelCase_   :     List[str]=18   ,	UpperCamelCase_   :     Union[str, Any]=30   ,	UpperCamelCase_   :     int=400   ,	UpperCamelCase_   :     List[str]=True   ,	UpperCamelCase_   :     Union[str, Any]=None   ,	UpperCamelCase_   :     Any=True   ,	UpperCamelCase_   :     List[str]=False   ,	UpperCamelCase_   :     Dict=True   ,	UpperCamelCase_   :     List[Any]=True   ,	UpperCamelCase_   :     List[Any]=[0.5, 0.5, 0.5]   ,	UpperCamelCase_   :     List[Any]=[0.5, 0.5, 0.5]   ,	):


						"""simple docstring"""




						__UpperCAmelCase						:			Tuple												=       parent
						__UpperCAmelCase						:			Dict												=       batch_size
						__UpperCAmelCase						:			List[Any]												=       num_channels
						__UpperCAmelCase						:			Tuple												=       image_size
						__UpperCAmelCase						:			Optional[Any]												=       min_resolution
						__UpperCAmelCase						:			Optional[Any]												=       max_resolution
						__UpperCAmelCase						:			List[Any]												=       do_resize
						__UpperCAmelCase						:			Tuple												=       size if size is not None else {"height": 18, "width": 20}
						__UpperCAmelCase						:			List[Any]												=       do_thumbnail
						__UpperCAmelCase						:			str												=       do_align_axis
						__UpperCAmelCase						:			int												=       do_pad
						__UpperCAmelCase						:			Optional[int]												=       do_normalize
						__UpperCAmelCase						:			List[Any]												=       image_mean
						__UpperCAmelCase						:			int												=       image_std


			def    a_  (   self   :     List[str]):


						"""simple docstring"""




						return {
						    "do_resize": self.do_resize,
						    "size": self.size,
						    "do_thumbnail": self.do_thumbnail,
						    "do_align_long_axis": self.do_align_axis,
						    "do_pad": self.do_pad,
						    "do_normalize": self.do_normalize,
						    "image_mean": self.image_mean,
						    "image_std": self.image_std,
						}






@require_torch
@require_vision
class   a__					(						__magic_name__				,     unittest.TestCase ):
			lowercase_		=						DonutImageProcessor if is_vision_available() else None


			def    a_  (   self   :     Optional[Any]):


						"""simple docstring"""




						__UpperCAmelCase						:			str												=       DonutImageProcessingTester(self)


			@property
			def    a_  (   self   :     List[Any]):


						"""simple docstring"""




						return self.image_processor_tester.prepare_image_processor_dict()


			def    a_  (   self   :     List[str]):


						"""simple docstring"""




						__UpperCAmelCase						:			Optional[Any]												=       self.image_processing_class(**self.image_processor_dict)
						self.assertTrue(hasattr(UpperCamelCase_   ,	"do_resize"))
						self.assertTrue(hasattr(UpperCamelCase_   ,	"size"))
						self.assertTrue(hasattr(UpperCamelCase_   ,	"do_thumbnail"))
						self.assertTrue(hasattr(UpperCamelCase_   ,	"do_align_long_axis"))
						self.assertTrue(hasattr(UpperCamelCase_   ,	"do_pad"))
						self.assertTrue(hasattr(UpperCamelCase_   ,	"do_normalize"))
						self.assertTrue(hasattr(UpperCamelCase_   ,	"image_mean"))
						self.assertTrue(hasattr(UpperCamelCase_   ,	"image_std"))


			def    a_  (   self   :     Union[str, Any]):


						"""simple docstring"""




						__UpperCAmelCase						:			Optional[int]												=       self.image_processing_class.from_dict(self.image_processor_dict)
						self.assertEqual(image_processor.size   ,	{"height": 18, "width": 20})

						__UpperCAmelCase						:			str												=       self.image_processing_class.from_dict(self.image_processor_dict   ,	size=42)
						self.assertEqual(image_processor.size   ,	{"height": 42, "width": 42})

						# Previous config had dimensions in (width, height) order
						__UpperCAmelCase						:			Tuple												=       self.image_processing_class.from_dict(self.image_processor_dict   ,	size=(42, 84))
						self.assertEqual(image_processor.size   ,	{"height": 84, "width": 42})


			def    a_  (   self   :     Optional[Any]):


						"""simple docstring"""




						pass


			@is_flaky()
			def    a_  (   self   :     Dict):


						"""simple docstring"""




						__UpperCAmelCase						:			List[str]												=       self.image_processing_class(**self.image_processor_dict)
						# create random PIL images
						__UpperCAmelCase						:			Dict												=       prepare_image_inputs(self.image_processor_tester   ,	equal_resolution=UpperCamelCase_)
						for image in image_inputs:
									self.assertIsInstance(UpperCamelCase_   ,	Image.Image)

						# Test not batched input
						__UpperCAmelCase						:			Union[str, Any]												=       image_processing(image_inputs[0]   ,	return_tensors="pt").pixel_values
						self.assertEqual(
						    encoded_images.shape   ,	(
						        1,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.size["height"],
						        self.image_processor_tester.size["width"],
						    )   ,	)

						# Test batched
						__UpperCAmelCase						:			List[Any]												=       image_processing(UpperCamelCase_   ,	return_tensors="pt").pixel_values
						self.assertEqual(
						    encoded_images.shape   ,	(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.size["height"],
						        self.image_processor_tester.size["width"],
						    )   ,	)


			@is_flaky()
			def    a_  (   self   :     Tuple):


						"""simple docstring"""




						__UpperCAmelCase						:			Optional[int]												=       self.image_processing_class(**self.image_processor_dict)
						# create random numpy tensors
						__UpperCAmelCase						:			Dict												=       prepare_image_inputs(self.image_processor_tester   ,	equal_resolution=UpperCamelCase_   ,	numpify=UpperCamelCase_)
						for image in image_inputs:
									self.assertIsInstance(UpperCamelCase_   ,	np.ndarray)

						# Test not batched input
						__UpperCAmelCase						:			Dict												=       image_processing(image_inputs[0]   ,	return_tensors="pt").pixel_values
						self.assertEqual(
						    encoded_images.shape   ,	(
						        1,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.size["height"],
						        self.image_processor_tester.size["width"],
						    )   ,	)

						# Test batched
						__UpperCAmelCase						:			List[str]												=       image_processing(UpperCamelCase_   ,	return_tensors="pt").pixel_values
						self.assertEqual(
						    encoded_images.shape   ,	(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.size["height"],
						        self.image_processor_tester.size["width"],
						    )   ,	)


			@is_flaky()
			def    a_  (   self   :     Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase						:			str												=       self.image_processing_class(**self.image_processor_dict)
						# create random PyTorch tensors
						__UpperCAmelCase						:			Optional[Any]												=       prepare_image_inputs(self.image_processor_tester   ,	equal_resolution=UpperCamelCase_   ,	torchify=UpperCamelCase_)
						for image in image_inputs:
									self.assertIsInstance(UpperCamelCase_   ,	torch.Tensor)

						# Test not batched input
						__UpperCAmelCase						:			str												=       image_processing(image_inputs[0]   ,	return_tensors="pt").pixel_values
						self.assertEqual(
						    encoded_images.shape   ,	(
						        1,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.size["height"],
						        self.image_processor_tester.size["width"],
						    )   ,	)

						# Test batched
						__UpperCAmelCase						:			List[Any]												=       image_processing(UpperCamelCase_   ,	return_tensors="pt").pixel_values
						self.assertEqual(
						    encoded_images.shape   ,	(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.size["height"],
						        self.image_processor_tester.size["width"],
						    )   ,	)






__UpperCamelCase     		=							2_5_6
# Modulus to hash a string
__UpperCamelCase     		=							1_0_0_0_0_0_3
def 							UpperCamelCase_(					_A :str					,  _A :str	)->		bool:
			UpperCamelCase__       =							len(_A	)
			UpperCamelCase__       =							len(_A	)
			if p_len > t_len:
						return False

			UpperCamelCase__       =							0
			UpperCamelCase__       =							0
			UpperCamelCase__       =							1

			# Calculating the hash of pattern and substring of text
			for i in range(_A	):
						UpperCamelCase__       =							(ord(pattern[i]	) + p_hash * alphabet_size) % modulus
						UpperCamelCase__       =							(ord(text[i]	) + text_hash * alphabet_size) % modulus
						if i == p_len - 1:
									continue
						UpperCamelCase__       =							(modulus_power * alphabet_size) % modulus

			for i in range(0					,  t_len - p_len + 1	):
						if text_hash == p_hash and text[i : i + p_len] == pattern:
									return True
						if i == t_len - p_len:
									continue
						# Calculate the https://en.wikipedia.org/wiki/Rolling_hash
						UpperCamelCase__       =							(
						    (text_hash - ord(text[i]	) * modulus_power) * alphabet_size
						    + ord(text[i + p_len]	)
						) % modulus
			return False
def 							UpperCamelCase_(					)->		None:
			UpperCamelCase__       =							"abc1abc12"
			UpperCamelCase__       =							"alskfjaldsabc1abc1abc12k23adsfabcabc"
			UpperCamelCase__       =							"alskfjaldsk23adsfabcabc"
			assert rabin_karp(_A					,  _A	) and not rabin_karp(_A					,  _A	)

			# Test 2)
			UpperCamelCase__       =							"ABABX"
			UpperCamelCase__       =							"ABABZABABYABABX"
			assert rabin_karp(_A					,  _A	)

			# Test 3)
			UpperCamelCase__       =							"AAAB"
			UpperCamelCase__       =							"ABAAAAAB"
			assert rabin_karp(_A					,  _A	)

			# Test 4)
			UpperCamelCase__       =							"abcdabcy"
			UpperCamelCase__       =							"abcxabcdabxabcdabcdabcy"
			assert rabin_karp(_A					,  _A	)

			# Test 5)
			UpperCamelCase__       =							"Lü"
			UpperCamelCase__       =							"Lüsai"
			assert rabin_karp(_A					,  _A	)
			UpperCamelCase__       =							"Lue"
			assert not rabin_karp(_A					,  _A	)
			print("Success."	)


if __name__ == "__main__":
						test_rabin_karp()


