Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. /* Stack_v2.c, implementing a more advanced stack */
  2.  
  3. /* Module Test function */
  4. void Test(void) {
  5. const uint8_t values[10] = {1,2,3,4,5,6,7,8,9,10};
  6. int i;
  7. uint8_t val;
  8. StackError_t res;
  9.  
  10. res = Init(); /* initialize stack */
  11. if (res!=STACK_NO_ERROR) {
  12. for (;;) {}/* error */
  13. }
  14. /* stack is empty here, test it */
  15. if (!isEmpty()) {
  16. for (;;) {}/* error */
  17. }
  18. /* push values on stack */
  19. for(i=0;i<sizeof(values)/sizeof(values[0]);i++) {
  20. res = Push(values[i]);
  21. if (res!=STACK_NO_ERROR) {
  22. for (;;) {}/* error */
  23. }
  24. }
  25. /* stack is full here, test it */
  26. if (!isFull()) {
  27. for (;;) {}/* error */
  28. }
  29. /* stack must be full here: test error case */
  30. res = Push(123);
  31. if (res!=STACK_OVERFLOW_ERROR) {
  32. for(;;){} /* error! */
  33. }
  34. /* get elements from the stack */
  35. for(i=sizeof(values)/sizeof(values[0])-1;i>=0;i--) {
  36. res = Peek(&val);
  37. if (res!=STACK_NO_ERROR || val!=values[i]) {
  38. for(;;){} /* error! */
  39. }
  40. res = Pop(&val);
  41. if (res!=STACK_NO_ERROR || val!=values[i]) {
  42. for(;;){} /* error! */
  43. }
  44. }
  45. /* stack is empty here, test it */
  46. if (!isEmpty()) {
  47. for (;;) {}/* error */
  48. }
  49. /* test error cases: stack is empty here */
  50. res = Peek(&val);
  51. if (res!=STACK_UNDERFLOW_ERROR) {
  52. for(;;){} /* error! */
  53. }
  54. res = Pop(&val);
  55. if (res!=STACK_UNDERFLOW_ERROR) {
  56. for(;;){} /* error! */
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement