string-stream-test.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * KUnit test for struct string_stream.
  4. *
  5. * Copyright (C) 2019, Google LLC.
  6. * Author: Brendan Higgins <brendanhiggins@google.com>
  7. */
  8. #include <kunit/test.h>
  9. #include <linux/slab.h>
  10. #include "string-stream.h"
  11. static void string_stream_test_empty_on_creation(struct kunit *test)
  12. {
  13. struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL);
  14. KUNIT_EXPECT_TRUE(test, string_stream_is_empty(stream));
  15. }
  16. static void string_stream_test_not_empty_after_add(struct kunit *test)
  17. {
  18. struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL);
  19. string_stream_add(stream, "Foo");
  20. KUNIT_EXPECT_FALSE(test, string_stream_is_empty(stream));
  21. }
  22. static void string_stream_test_get_string(struct kunit *test)
  23. {
  24. struct string_stream *stream = alloc_string_stream(test, GFP_KERNEL);
  25. char *output;
  26. string_stream_add(stream, "Foo");
  27. string_stream_add(stream, " %s", "bar");
  28. output = string_stream_get_string(stream);
  29. KUNIT_ASSERT_STREQ(test, output, "Foo bar");
  30. }
  31. static struct kunit_case string_stream_test_cases[] = {
  32. KUNIT_CASE(string_stream_test_empty_on_creation),
  33. KUNIT_CASE(string_stream_test_not_empty_after_add),
  34. KUNIT_CASE(string_stream_test_get_string),
  35. {}
  36. };
  37. static struct kunit_suite string_stream_test_suite = {
  38. .name = "string-stream-test",
  39. .test_cases = string_stream_test_cases
  40. };
  41. kunit_test_suites(&string_stream_test_suite);