Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit cb15ce3

Browse files
author
Rohit Kumar Srivastava
committed
code refactor and added comments
1 parent 43ccbd2 commit cb15ce3

File tree

1 file changed

+131
-139
lines changed

1 file changed

+131
-139
lines changed

tests/nightly/test_large_vector.py

Lines changed: 131 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -733,155 +733,147 @@ def test_repeat():
733733

734734

735735
def create_input_for_rounding_ops():
736+
# Creates an vector with values (-LARGE/2 .... -2, -1, 0, 1, 2, .... , LARGE/2-1)
737+
# then divides each element by 2 i.e (-LARGE/4 .... -1, -0.5, 0, 0.5, 1, .... , LARGE/4-1)
736738
inp = nd.arange(-LARGE_X//2, LARGE_X//2, dtype=np.float64)
737739
inp = inp/2
738740
return inp
739741

740742

741-
def test_ceil():
742-
x = create_input_for_rounding_ops()
743-
y = nd.ceil(x)
744-
assert y[LARGE_X//2-2] == -1
745-
assert y[LARGE_X//2-1] == 0
746-
assert y[LARGE_X//2] == 0
747-
assert y[LARGE_X//2+1] == 1
748-
assert y[LARGE_X//2+2] == 1
749-
750-
751-
def test_fix():
752-
x = create_input_for_rounding_ops()
753-
y = nd.fix(x)
754-
assert y[LARGE_X//2-2] == -1
755-
assert y[LARGE_X//2-1] == 0
756-
assert y[LARGE_X//2] == 0
757-
assert y[LARGE_X//2+1] == 0
758-
assert y[LARGE_X//2+2] == 1
759-
760-
761-
def test_floor():
762-
x = create_input_for_rounding_ops()
763-
y = nd.floor(x)
764-
assert y[LARGE_X//2-2] == -1
765-
assert y[LARGE_X//2-1] == -1
766-
assert y[LARGE_X//2] == 0
767-
assert y[LARGE_X//2+1] == 0
768-
assert y[LARGE_X//2+2] == 1
743+
def assert_correctness_of_rounding_ops(output, mid, expected_vals):
744+
# checks verifies 5 values at the middle positions of the input vector
745+
# i.e mid-2, mid-1, mid, mid+1, mid+2
746+
output_idx_to_inspect = [mid-2, mid-1, mid, mid+1, mid+2]
747+
for i in range(len(output_idx_to_inspect)):
748+
assert output[output_idx_to_inspect[i]] == expected_vals[i]
769749

770750

771-
def test_rint():
751+
def test_rounding_ops():
772752
x = create_input_for_rounding_ops()
773-
y = nd.rint(x)
774-
assert y[LARGE_X//2-2] == -1
775-
assert y[LARGE_X//2-1] == -1
776-
assert y[LARGE_X//2] == 0
777-
assert y[LARGE_X//2+1] == 0
778-
assert y[LARGE_X//2+2] == 1
779-
780-
781-
def test_round():
782-
x = create_input_for_rounding_ops()
783-
y = nd.round(x)
784-
assert y[LARGE_X//2-2] == -1
785-
assert y[LARGE_X//2-1] == -1
786-
assert y[LARGE_X//2] == 0
787-
assert y[LARGE_X//2+1] == 1
788-
assert y[LARGE_X//2+2] == 1
753+
754+
def test_ceil():
755+
y = nd.ceil(x)
756+
# expected ouput for middle 5 values after applying ceil()
757+
expected_output = [-1, 0, 0, 1, 1]
758+
assert_correctness_of_rounding_ops(y, LARGE_X//2, expected_output)
759+
760+
def test_fix():
761+
y = nd.fix(x)
762+
# expected ouput for middle 5 values after applying fix()
763+
expected_output = [-1, 0, 0, 0, 1]
764+
assert_correctness_of_rounding_ops(y, LARGE_X//2, expected_output)
765+
766+
def test_floor():
767+
y = nd.floor(x)
768+
# expected ouput for middle 5 values after applying floor()
769+
expected_output = [-1, -1, 0, 0, 1]
770+
assert_correctness_of_rounding_ops(y, LARGE_X//2, expected_output)
771+
772+
def test_rint():
773+
y = nd.rint(x)
774+
# expected ouput for middle 5 values after applying rint()
775+
expected_output = [-1, -1, 0, 0, 1]
776+
assert_correctness_of_rounding_ops(y, LARGE_X//2, expected_output)
777+
778+
def test_round():
779+
y = nd.round(x)
780+
# expected ouput for middle 5 values after applying round()
781+
expected_output = [-1, -1, 0, 1, 1]
782+
assert_correctness_of_rounding_ops(y, LARGE_X//2, expected_output)
783+
784+
def test_trunc():
785+
y = nd.trunc(x)
786+
# expected ouput for middle 5 values after applying trunc()
787+
expected_output = [-1, 0, 0, 0, 1]
788+
assert_correctness_of_rounding_ops(y, LARGE_X//2, expected_output)
789+
790+
test_ceil()
791+
test_fix()
792+
test_floor()
793+
test_rint()
794+
test_round()
795+
test_trunc()
796+
797+
798+
def create_input_for_trigonometric_ops(vals):
799+
# Creates large vector input of size(LARGE_X) from vals using tile operator
800+
inp = nd.array(vals)
801+
inp = nd.tile(inp, LARGE_X//len(vals))
802+
return inp
789803

790804

791-
def test_trunc():
792-
x = create_input_for_rounding_ops()
793-
y = nd.trunc(x)
794-
assert y[LARGE_X//2-2] == -1
795-
assert y[LARGE_X//2-1] == 0
796-
assert y[LARGE_X//2] == 0
797-
assert y[LARGE_X//2+1] == 0
798-
assert y[LARGE_X//2+2] == 1
799-
800-
801-
def test_arcsin():
802-
x = nd.array([-1, -.707, 0, .707, 1])
803-
x = nd.tile(x, LARGE_X//5)
804-
y = nd.arcsin(x)
805-
assert_almost_equal(y[0].asnumpy(), -np.pi/2, atol=1e-3)
806-
assert_almost_equal(y[1].asnumpy(), -np.pi/4, atol=1e-3)
807-
assert_almost_equal(y[-3].asnumpy(), 0, atol=1e-3)
808-
assert_almost_equal(y[-2].asnumpy(), np.pi/4, atol=1e-3)
809-
assert_almost_equal(y[-1].asnumpy(), np.pi/2, atol=1e-3)
810-
811-
812-
def test_arccos():
813-
x = nd.array([-1, -.707, 0, .707, 1])
814-
x = nd.tile(x, LARGE_X//5)
815-
y = nd.arccos(x)
816-
assert_almost_equal(y[0].asnumpy(), np.pi, atol=1e-3)
817-
assert_almost_equal(y[1].asnumpy(), 3*np.pi/4, atol=1e-3)
818-
assert_almost_equal(y[-3].asnumpy(), np.pi/2, atol=1e-3)
819-
assert_almost_equal(y[-2].asnumpy(), np.pi/4, atol=1e-3)
820-
assert_almost_equal(y[-1].asnumpy(), 0, atol=1e-3)
821-
822-
823-
def test_arctan():
824-
x = nd.array([-np.Inf, -1, 0, 1, np.Inf])
825-
x = nd.tile(x, LARGE_X//5)
826-
y = nd.arctan(x)
827-
assert_almost_equal(y[0].asnumpy(), -np.pi/2, atol=1e-3)
828-
assert_almost_equal(y[1].asnumpy(), -np.pi/4, atol=1e-3)
829-
assert_almost_equal(y[-3].asnumpy(), 0, atol=1e-3)
830-
assert_almost_equal(y[-2].asnumpy(), np.pi/4, atol=1e-3)
831-
assert_almost_equal(y[-1].asnumpy(), np.pi/2, atol=1e-3)
832-
833-
834-
def test_sin():
835-
x = nd.array([-np.pi/2, -np.pi/4, 0, np.pi/4, np.pi/2])
836-
x = nd.tile(x, LARGE_X//5)
837-
y = nd.sin(x)
838-
assert_almost_equal(y[0].asnumpy(), -1, atol=1e-3)
839-
assert_almost_equal(y[1].asnumpy(), -.707, atol=1e-3)
840-
assert_almost_equal(y[-3].asnumpy(), 0, atol=1e-3)
841-
assert_almost_equal(y[-2].asnumpy(), .707, atol=1e-3)
842-
assert_almost_equal(y[-1].asnumpy(), 1, atol=1e-3)
843-
844-
845-
def test_cos():
846-
x = nd.array([0, np.pi/4, np.pi/2, 3*np.pi/4, np.pi])
847-
x = nd.tile(x, LARGE_X//5)
848-
y = nd.cos(x)
849-
assert_almost_equal(y[0].asnumpy(), 1, atol=1e-3)
850-
assert_almost_equal(y[1].asnumpy(), .707, atol=1e-3)
851-
assert_almost_equal(y[-3].asnumpy(), 0, atol=1e-3)
852-
assert_almost_equal(y[-2].asnumpy(), -.707, atol=1e-3)
853-
assert_almost_equal(y[-1].asnumpy(), -1, atol=1e-3)
854-
855-
856-
def test_tan():
857-
x = nd.array([-np.pi/4, 0, np.pi/4])
858-
x = nd.tile(x, LARGE_X//3)
859-
y = nd.tan(x)
860-
assert y[0] == -1
861-
assert y[1] == 0
862-
assert y[-1] == 1
863-
864-
865-
def test_radians():
866-
x = nd.array([0, 90, 180, 270, 360])
867-
x = nd.tile(x, LARGE_X//5)
868-
y = nd.radians(x)
869-
assert_almost_equal(y[0].asnumpy(), 0, atol=1e-3)
870-
assert_almost_equal(y[1].asnumpy(), np.pi/2, atol=1e-3)
871-
assert_almost_equal(y[-3].asnumpy(), np.pi, atol=1e-3)
872-
assert_almost_equal(y[-2].asnumpy(), 3*np.pi/2, atol=1e-3)
873-
assert_almost_equal(y[-1].asnumpy(), 2*np.pi, atol=1e-3)
874-
875-
876-
def test_degrees():
877-
x = nd.array([0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi])
878-
x = nd.tile(x, LARGE_X//5)
879-
y = nd.degrees(x)
880-
assert_almost_equal(y[0].asnumpy(), 0, atol=1e-3)
881-
assert_almost_equal(y[1].asnumpy(), 90, atol=1e-3)
882-
assert_almost_equal(y[-3].asnumpy(), 180, atol=1e-3)
883-
assert_almost_equal(y[-2].asnumpy(), 270, atol=1e-3)
884-
assert_almost_equal(y[-1].asnumpy(), 360, atol=1e-3)
805+
def assert_correctness_of_trigonometric_ops(output, expected_vals):
806+
# checks verifies 5 values at positions(0, 1, -3, -2, -1) of the input vector
807+
output_idx_to_inspect = [0, 1, -3, -2, -1]
808+
for i in range(len(output_idx_to_inspect)):
809+
assert np.abs(output[output_idx_to_inspect[i]].asnumpy()-expected_vals[i]) <= 1e-3
810+
811+
812+
def test_trigonometric_ops():
813+
def test_arcsin():
814+
x = create_input_for_trigonometric_ops([-1, -.707, 0, .707, 1])
815+
y = nd.arcsin(x)
816+
# expected ouput for indices=(0, 1, -3, -2, -1) after applying arcsin()
817+
expected_output = [-np.pi/2, -np.pi/4, 0, np.pi/4, np.pi/2]
818+
assert_correctness_of_trigonometric_ops(y, expected_output)
819+
820+
def test_arccos():
821+
x = create_input_for_trigonometric_ops([-1, -.707, 0, .707, 1])
822+
y = nd.arccos(x)
823+
# expected ouput for indices=(0, 1, -3, -2, -1) after applying arccos()
824+
expected_output = [np.pi, 3*np.pi/4, np.pi/2, np.pi/4, 0]
825+
assert_correctness_of_trigonometric_ops(y, expected_output)
826+
827+
def test_arctan():
828+
x = create_input_for_trigonometric_ops([-np.Inf, -1, 0, 1, np.Inf])
829+
y = nd.arctan(x)
830+
# expected ouput for indices=(0, 1, -3, -2, -1) after applying arctan()
831+
expected_output = [-np.pi/2, -np.pi/4, 0, np.pi/4, np.pi/2]
832+
assert_correctness_of_trigonometric_ops(y, expected_output)
833+
834+
def test_sin():
835+
x = create_input_for_trigonometric_ops([-np.pi/2, -np.pi/4, 0, np.pi/4, np.pi/2])
836+
y = nd.sin(x)
837+
# expected ouput for indices=(0, 1, -3, -2, -1) after applying sin()
838+
expected_output = [-1, -.707, 0, .707, 1]
839+
assert_correctness_of_trigonometric_ops(y, expected_output)
840+
841+
def test_cos():
842+
x = create_input_for_trigonometric_ops([0, np.pi/4, np.pi/2, 3*np.pi/4, np.pi])
843+
y = nd.cos(x)
844+
# expected ouput for indices=(0, 1, -3, -2, -1) after applying cos()
845+
expected_output = [1, .707, 0, -.707, -1]
846+
assert_correctness_of_trigonometric_ops(y, expected_output)
847+
848+
def test_tan():
849+
x = create_input_for_trigonometric_ops([-np.pi/6, -np.pi/4, 0, np.pi/4, np.pi/6])
850+
y = nd.tan(x)
851+
# expected ouput for indices=(0, 1, -3, -2, -1) after applying tan()
852+
expected_output = [-.577, -1, 0, 1, .577]
853+
assert_correctness_of_trigonometric_ops(y, expected_output)
854+
855+
def test_radians():
856+
x = create_input_for_trigonometric_ops([0, 90, 180, 270, 360])
857+
y = nd.radians(x)
858+
# expected ouput for indices=(0, 1, -3, -2, -1) after applying radians()
859+
expected_output = [0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi]
860+
assert_correctness_of_trigonometric_ops(y, expected_output)
861+
862+
def test_degrees():
863+
x = create_input_for_trigonometric_ops([0, np.pi/2, np.pi, 3*np.pi/2, 2*np.pi])
864+
y = nd.degrees(x)
865+
# expected ouput for indices=(0, 1, -3, -2, -1) after applying degrees()
866+
expected_output = [0, 90, 180, 270, 360]
867+
assert_correctness_of_trigonometric_ops(y, expected_output)
868+
869+
test_arcsin()
870+
test_arccos()
871+
test_arctan()
872+
test_sin()
873+
test_cos()
874+
test_tan()
875+
test_radians()
876+
test_degrees()
885877

886878

887879
if __name__ == '__main__':

0 commit comments

Comments
 (0)