Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.9KB

  1. /*
  2. * Carla Tests
  3. * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the GPL.txt file
  16. */
  17. #include "CarlaBase64Utils.hpp"
  18. #include "CarlaString.hpp"
  19. int main()
  20. {
  21. CARLA_ASSERT(std::strlen(kBase64) == 64);
  22. struct Blob {
  23. char s[4];
  24. int i;
  25. double d;
  26. char padding[100];
  27. void* ptr;
  28. Blob()
  29. : s{'1', 's', 't', 0},
  30. i(228),
  31. d(3.33333333333),
  32. ptr((void*)0x500) {}
  33. } blob;
  34. // binary -> base64
  35. void* const test0 = &blob;
  36. size_t test0Len = sizeof(Blob);
  37. char buf0[carla_base64_encoded_len(test0Len) + 1];
  38. carla_base64_encode((const uint8_t*)test0, test0Len, buf0);
  39. printf("BINARY '%s'\n", buf0);
  40. char buf0dec[carla_base64_decoded_max_len(buf0)];
  41. carla_base64_decode(buf0, (uint8_t*)buf0dec);
  42. Blob blobTester;
  43. blobTester.s[0] = 0;
  44. blobTester.i = 0;
  45. blobTester.d = 9999.99999999999999;
  46. std::memcpy(&blobTester, buf0dec, sizeof(Blob));
  47. CARLA_ASSERT(std::strcmp(blobTester.s, "1st") == 0);
  48. CARLA_ASSERT(blobTester.i == 228);
  49. CARLA_ASSERT(blobTester.d == 3.33333333333);
  50. // string -> base64
  51. const char* const test1 = "Hello World!";
  52. size_t test1Len = std::strlen(test1);
  53. char buf1[carla_base64_encoded_len(test1Len) + 1];
  54. carla_base64_encode((const uint8_t*)test1, test1Len, buf1);
  55. printf("'%s' => '%s'\n", test1, buf1);
  56. // base64 -> string
  57. const char* const test2 = "SGVsbG8gV29ybGQh";
  58. char buf2[carla_base64_decoded_max_len(test2)];
  59. carla_base64_decode(test2, (uint8_t*)buf2);
  60. printf("'%s' => '%s'\n", test2, buf2);
  61. printf("'%s' == '%s'\n", buf1, test2);
  62. printf("'%s' == '%s'\n", buf2, test1);
  63. CARLA_ASSERT(std::strcmp(buf1, test2) == 0);
  64. CARLA_ASSERT(std::strcmp(buf2, test1) == 0);
  65. // -----------------------------------------------------------------
  66. blob.s[0] = 'X';
  67. blob.i = 92320;
  68. blob.d = 9999887.99999999999999;
  69. CarlaString string;
  70. string.importBinaryAsBase64<Blob>(&blob);
  71. Blob* blob3 = string.exportAsBase64Binary<Blob>();
  72. CARLA_ASSERT(std::strcmp(blob3->s, blob.s) == 0);
  73. CARLA_ASSERT(blob3->i == blob.i);
  74. CARLA_ASSERT(blob3->d == blob.d);
  75. CARLA_ASSERT(blob3->ptr == blob.ptr);
  76. delete blob3;
  77. // -----------------------------------------------------------------
  78. return 0;
  79. }