1
0

rand.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-11-17 Bernard first version
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <finsh.h>
  13. int libc_rand(void)
  14. {
  15. int i1, i2;
  16. int j1, j2;
  17. /* The C standard says that "If rand is called before any calls to
  18. srand have been made, the same sequence shall be generated as
  19. when srand is first called with a seed value of 1." */
  20. i1 = rand();
  21. i2 = rand();
  22. srand(1);
  23. j1 = rand();
  24. j2 = rand();
  25. if (i1 < 0 || i2 < 0 || j1 < 0 || j2 < 0)
  26. {
  27. puts("Test FAILED!");
  28. }
  29. if (j1 == i1 && j2 == i2)
  30. {
  31. puts("Test succeeded.");
  32. return 0;
  33. }
  34. else
  35. {
  36. if (j1 != i1)
  37. printf("%d != %d\n", j1, i1);
  38. if (j2 != i2)
  39. printf("%d != %d\n", j2, i2);
  40. puts("Test FAILED!");
  41. return 1;
  42. }
  43. }
  44. FINSH_FUNCTION_EXPORT(libc_rand, rand test for libc);