test_fifo.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * test_fifo.c
  3. *
  4. * Created on: Jan 15, 2013
  5. * Author: hathach
  6. */
  7. /*
  8. * Software License Agreement (BSD License)
  9. * Copyright (c) 2012, hathach (tinyusb.net)
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  26. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  31. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  32. * OF SUCH DAMAGE.
  33. *
  34. * This file is part of the tiny usb stack.
  35. */
  36. #include "unity.h"
  37. #include "fifo.h"
  38. #define FIFO_SIZE 10
  39. static fifo_t ff;
  40. static uint8_t buffer[FIFO_SIZE];
  41. void setUp(void)
  42. {
  43. fifo_init(&ff, buffer, FIFO_SIZE, 0, 0);
  44. }
  45. void tearDown(void)
  46. {
  47. memset(&ff, 0, sizeof(fifo_t));
  48. }
  49. void test_create_null(void)
  50. {
  51. memset(&ff, 0, sizeof(fifo_t)); // clear fifo to test null created
  52. TEST_ASSERT_FALSE( fifo_init(&ff, buffer, 0, 0, 0) );
  53. TEST_ASSERT_TRUE( fifo_init(&ff, buffer, 1, 0, 0) );
  54. }
  55. void test_normal(void)
  56. {
  57. uint8_t i;
  58. for(i=0; i < FIFO_SIZE; i++)
  59. {
  60. fifo_write(&ff, i);
  61. }
  62. for(i=0; i < FIFO_SIZE; i++)
  63. {
  64. uint8_t c;
  65. fifo_read(&ff, &c);
  66. TEST_ASSERT_EQUAL(i, c);
  67. }
  68. }
  69. void test_is_empty(void)
  70. {
  71. TEST_ASSERT_TRUE(fifo_is_empty(&ff));
  72. fifo_write(&ff, 1);
  73. TEST_ASSERT_FALSE(fifo_is_empty(&ff));
  74. }
  75. void test_is_full(void)
  76. {
  77. uint8_t i;
  78. TEST_ASSERT_FALSE(fifo_is_full(&ff));
  79. for(i=0; i < FIFO_SIZE; i++)
  80. {
  81. fifo_write(&ff, i);
  82. }
  83. TEST_ASSERT_TRUE(fifo_is_full(&ff));
  84. }