benc_serializeInteger_test.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/Allocator.h"
  16. #include "memory/MallocAllocator.h"
  17. #include "io/Reader.h"
  18. #include "io/ArrayReader.h"
  19. #include "io/Writer.h"
  20. #include "io/ArrayWriter.h"
  21. #include "benc/Object.h"
  22. #include "benc/serialization/BencSerializer.h"
  23. #include "benc/serialization/standard/StandardBencSerializer.h"
  24. #include "util/CString.h"
  25. #include <stdio.h>
  26. static int expect(char* str, struct Writer* writer, struct Reader* reader, int ret)
  27. {
  28. char buffer[32];
  29. Writer_write(writer, "\0", 1);
  30. Reader_read(reader, buffer, CString_strlen(str) + 1);
  31. if (CString_strcmp(str, buffer) != 0) {
  32. printf("Expected %s\n Got %s\n", str, buffer);
  33. return -1;
  34. }
  35. return ret;
  36. }
  37. static int testSerialize(struct Writer* writer, struct Reader* reader)
  38. {
  39. int ret = 0;
  40. ret |= StandardBencSerializer_get()->serializeint64_t(writer, 1);
  41. ret |= expect("i1e", writer, reader, ret);
  42. ret |= StandardBencSerializer_get()->serializeint64_t(writer, 1000);
  43. ret |= expect("i1000e", writer, reader, ret);
  44. ret |= StandardBencSerializer_get()->serializeint64_t(writer, -100);
  45. ret |= expect("i-100e", writer, reader, ret);
  46. return ret;
  47. }
  48. int main()
  49. {
  50. char out[512];
  51. struct Allocator* alloc = MallocAllocator_new(1<<20);
  52. struct Writer* writer = ArrayWriter_new(out, 512, alloc);
  53. struct Reader* reader = ArrayReader_new(out, 512, alloc);
  54. int ret = testSerialize(writer, reader);
  55. Allocator_free(alloc);
  56. return ret;
  57. }