You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_task6.py 883 B

123456789101112131415161718192021222324252627282930
  1. from uctc.framework import basis
  2. import numpy as np
  3. from functools import reduce
  4. import random
  5. def is_close(x, y):
  6. return abs(x - y) < 1e-3
  7. arr = [random.random() for i in range(128)]
  8. test_x1 = basis.sumList(arr)
  9. test_x2 = basis.prodList(arr)
  10. test_y1 = reduce(lambda x, y: x + y, arr, 0.0)
  11. test_y2 = reduce(lambda x, y: x * y, arr, 1.0)
  12. if not is_close(test_x1, test_y1):
  13. print(f"\033[1;31mError: {basis.sumList.__name__} failed test... expects {test_y1} but gets {test_x1}\033[0m")
  14. exit(0)
  15. print(f"\033[1;34mPassed: {basis.sumList.__name__} passed all tests\033[0m")
  16. if not is_close(test_x2, test_y2):
  17. print(f"\033[1;31mError: {basis.prodList.__name__} failed test... expects {test_y2} but gets {test_x2}\033[0m")
  18. exit(0)
  19. print(f"\033[1;34mPassed: {basis.prodList.__name__} passed all tests\033[0m")
  20. print(f"\033[1;32m[PASSED] Task 3 finished!\033[0m")

计算机大作业