Example #1
0
	def test_tas_exceptions(self, test_op):
		pytest.raises(
			ValueError,
			WAtomicCounter(0, negative=False).test_and_set,
			test_op,
			WAtomicCounter(-1)
		)
Example #2
0
	def test_multi_threading(self):
		c = WAtomicCounter()

		def thread_fn_increase():
			for i in range(self.__repeats__):
				c.increase_counter(1)

		threads = [threading.Thread(target=thread_fn_increase) for x in range(self.__threads__)]
		for th in threads:
			th.start()

		for th in threads:
			th.join()

		assert(c.__int__() == (self.__threads__ * self.__repeats__))
Example #3
0
	def test_set(self):
		counter = WAtomicCounter()
		assert(counter.set(5) == 0)
		assert(int(counter) == 5)

		counter = WAtomicCounter(1, negative=False)
		assert(counter.set(7) == 1)
		assert(int(counter) == 7)

		pytest.raises(ValueError, counter.set, -3)
Example #4
0
	def test_negative(self):
		WAtomicCounter()
		WAtomicCounter(-7)
		WAtomicCounter(-7, negative=True)
		pytest.raises(ValueError, WAtomicCounter, -7, negative=False)

		c = WAtomicCounter(negative=False)
		assert(int(WAtomicCounter()) == 0)

		c = WAtomicCounter(3, negative=False)
		assert(int(c) == 3)
		pytest.raises(ValueError, c.increase_counter, -10)
		assert(int(c) == 3)
Example #5
0
	def test_counter_maximum(self):
		u_long_long_bits_count = (8 * 8)

		WAtomicCounter((1 << u_long_long_bits_count) - 1)
		WAtomicCounter((1 << u_long_long_bits_count) + 10)
Example #6
0
	def test(self):

		c = WAtomicCounter()
		assert(c.__int__() == 0)

		c = WAtomicCounter()
		assert(c.__int__() == 0)

		c.increase_counter(1)
		assert(c.__int__() == 1)

		c.increase_counter(10)
		assert(c.__int__() == 11)

		c = WAtomicCounter(5)
		assert(c.__int__() == 5)

		c.increase_counter(1)
		assert(c.__int__() == 6)
Example #7
0
	def test_cas(self):
		counter = WAtomicCounter(1)
		assert(counter.compare_and_set(WAtomicCounter(5), WAtomicCounter(7)) is None)
		assert(int(counter) == 1)

		assert(counter.compare_and_set(WAtomicCounter(1), WAtomicCounter(-7)) == 1)
		assert(int(counter) == -7)

		counter = WAtomicCounter(3, negative=False)
		pytest.raises(ValueError, counter.compare_and_set, WAtomicCounter(3), WAtomicCounter(-5))
		assert(int(counter) == 3)
Example #8
0
	def test_tas(self, test_op, start_value, compare_value, tas_result, counter_result):
		counter = WAtomicCounter(start_value)
		tas = counter.test_and_set(test_op, WAtomicCounter(compare_value))
		assert((tas is tas_result) or (tas == tas_result))
		assert(int(counter) == counter_result)