/ externals / zycore / examples / String.c
String.c
  1  /***************************************************************************************************
  2  
  3    Zyan Core Library (Zycore-C)
  4  
  5    Original Author : Florian Bernd
  6  
  7   * Permission is hereby granted, free of charge, to any person obtaining a copy
  8   * of this software and associated documentation files (the "Software"), to deal
  9   * in the Software without restriction, including without limitation the rights
 10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11   * copies of the Software, and to permit persons to whom the Software is
 12   * furnished to do so, subject to the following conditions:
 13   *
 14   * The above copyright notice and this permission notice shall be included in all
 15   * copies or substantial portions of the Software.
 16   *
 17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 23   * SOFTWARE.
 24  
 25  ***************************************************************************************************/
 26  
 27  /**
 28   * @file
 29   * Demonstrates the `String` implementation.
 30   */
 31  
 32  #include <stdio.h>
 33  #include <Zycore/Allocator.h>
 34  #include <Zycore/Defines.h>
 35  #include <Zycore/LibC.h>
 36  #include <Zycore/String.h>
 37  #include <Zycore/Types.h>
 38  
 39  /* ============================================================================================== */
 40  /* Enums and types                                                                                */
 41  /* ============================================================================================== */
 42  
 43  
 44  /* ============================================================================================== */
 45  /* Helper functions                                                                               */
 46  /* ============================================================================================== */
 47  
 48  /* ============================================================================================== */
 49  /* Tests                                                                                          */
 50  /* ============================================================================================== */
 51  
 52  /* ---------------------------------------------------------------------------------------------- */
 53  /* Basic tests                                                                                    */
 54  /* ---------------------------------------------------------------------------------------------- */
 55  
 56  /**
 57   * Performs some basic test on the given `ZyanString` instance.
 58   *
 59   * @param   string  A pointer to the `ZyanString` instance.
 60   *
 61   * @return  A zyan status code.
 62   */
 63  static ZyanStatus PerformBasicTests(ZyanString* string)
 64  {
 65      ZYAN_ASSERT(string);
 66      ZYAN_UNUSED(string);
 67  
 68  
 69  
 70      return ZYAN_STATUS_SUCCESS;
 71  }
 72  
 73  /**
 74   * Performs basic tests on a string that dynamically manages memory.
 75   *
 76   * @return  A zyan status code.
 77   */
 78  static ZyanStatus TestDynamic(void)
 79  {
 80      PerformBasicTests(ZYAN_NULL);
 81      return ZYAN_STATUS_SUCCESS;
 82  }
 83  
 84  /**
 85   * Performs basic tests on a string that uses a static buffer.
 86   *
 87   * @return  A zyan status code.
 88   */
 89  static ZyanStatus TestStatic(void)
 90  {
 91      PerformBasicTests(ZYAN_NULL);
 92      return ZYAN_STATUS_SUCCESS;
 93  }
 94  
 95  /* ---------------------------------------------------------------------------------------------- */
 96  /* Custom allocator                                                                               */
 97  /* ---------------------------------------------------------------------------------------------- */
 98  
 99  //static ZyanStatus AllocatorAllocate(ZyanAllocator* allocator, void** p, ZyanUSize element_size,
100  //    ZyanUSize n)
101  //{
102  //    ZYAN_ASSERT(allocator);
103  //    ZYAN_ASSERT(p);
104  //    ZYAN_ASSERT(element_size);
105  //    ZYAN_ASSERT(n);
106  //
107  //    ZYAN_UNUSED(allocator);
108  //
109  //    *p = ZYAN_MALLOC(element_size * n);
110  //    if (!*p)
111  //    {
112  //        return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
113  //    }
114  //
115  //    return ZYAN_STATUS_SUCCESS;
116  //}
117  //
118  //static ZyanStatus AllocatorReallocate(ZyanAllocator* allocator, void** p, ZyanUSize element_size,
119  //    ZyanUSize n)
120  //{
121  //    ZYAN_ASSERT(allocator);
122  //    ZYAN_ASSERT(p);
123  //    ZYAN_ASSERT(element_size);
124  //    ZYAN_ASSERT(n);
125  //
126  //    ZYAN_UNUSED(allocator);
127  //
128  //    void* const x = ZYAN_REALLOC(*p, element_size * n);
129  //    if (!x)
130  //    {
131  //        return ZYAN_STATUS_NOT_ENOUGH_MEMORY;
132  //    }
133  //    *p = x;
134  //
135  //    return ZYAN_STATUS_SUCCESS;
136  //}
137  //
138  //static ZyanStatus AllocatorDeallocate(ZyanAllocator* allocator, void* p, ZyanUSize element_size,
139  //    ZyanUSize n)
140  //{
141  //    ZYAN_ASSERT(allocator);
142  //    ZYAN_ASSERT(p);
143  //    ZYAN_ASSERT(element_size);
144  //    ZYAN_ASSERT(n);
145  //
146  //    ZYAN_UNUSED(allocator);
147  //    ZYAN_UNUSED(element_size);
148  //    ZYAN_UNUSED(n);
149  //
150  //    ZYAN_FREE(p);
151  //
152  //    return ZYAN_STATUS_SUCCESS;
153  //}
154  
155  /* ---------------------------------------------------------------------------------------------- */
156  
157  /**
158   * Performs basic tests on a vector that dynamically manages memory using a custom
159   * allocator and modified growth-factor/shrink-threshold.
160   *
161   * @return  A zyan status code.
162   */
163  static ZyanStatus TestAllocator(void)
164  {
165      return ZYAN_STATUS_SUCCESS;
166  }
167  
168  /* ---------------------------------------------------------------------------------------------- */
169  
170  /* ============================================================================================== */
171  /* Entry point                                                                                    */
172  /* ============================================================================================== */
173  
174  int main()
175  {
176      if (!ZYAN_SUCCESS(TestDynamic()))
177      {
178          return EXIT_FAILURE;
179      }
180      if (!ZYAN_SUCCESS(TestStatic()))
181      {
182          return EXIT_FAILURE;
183      }
184      if (!ZYAN_SUCCESS(TestAllocator()))
185      {
186          return EXIT_FAILURE;
187      }
188  
189      return EXIT_SUCCESS;
190  }
191  
192  /* ============================================================================================== */