raw_printer_test.cc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
  2. // Copyright 2009 Google Inc. All Rights Reserved.
  3. // Author: sanjay@google.com (Sanjay Ghemawat)
  4. //
  5. // Use of this source code is governed by a BSD-style license that can
  6. // be found in the LICENSE file.
  7. #include "raw_printer.h"
  8. #include <stdio.h>
  9. #include <string>
  10. #include "base/logging.h"
  11. using std::string;
  12. #define TEST(a, b) void TEST_##a##_##b()
  13. #define RUN_TEST(a, b) TEST_##a##_##b()
  14. TEST(RawPrinter, Empty) {
  15. char buffer[1];
  16. base::RawPrinter printer(buffer, arraysize(buffer));
  17. CHECK_EQ(0, printer.length());
  18. CHECK_EQ(string(""), buffer);
  19. CHECK_EQ(0, printer.space_left());
  20. printer.Printf("foo");
  21. CHECK_EQ(string(""), string(buffer));
  22. CHECK_EQ(0, printer.length());
  23. CHECK_EQ(0, printer.space_left());
  24. }
  25. TEST(RawPrinter, PartiallyFilled) {
  26. char buffer[100];
  27. base::RawPrinter printer(buffer, arraysize(buffer));
  28. printer.Printf("%s %s", "hello", "world");
  29. CHECK_EQ(string("hello world"), string(buffer));
  30. CHECK_EQ(11, printer.length());
  31. CHECK_LT(0, printer.space_left());
  32. }
  33. TEST(RawPrinter, Truncated) {
  34. char buffer[3];
  35. base::RawPrinter printer(buffer, arraysize(buffer));
  36. printer.Printf("%d", 12345678);
  37. CHECK_EQ(string("12"), string(buffer));
  38. CHECK_EQ(2, printer.length());
  39. CHECK_EQ(0, printer.space_left());
  40. }
  41. TEST(RawPrinter, ExactlyFilled) {
  42. char buffer[12];
  43. base::RawPrinter printer(buffer, arraysize(buffer));
  44. printer.Printf("%s %s", "hello", "world");
  45. CHECK_EQ(string("hello world"), string(buffer));
  46. CHECK_EQ(11, printer.length());
  47. CHECK_EQ(0, printer.space_left());
  48. }
  49. int main(int argc, char **argv) {
  50. RUN_TEST(RawPrinter, Empty);
  51. RUN_TEST(RawPrinter, PartiallyFilled);
  52. RUN_TEST(RawPrinter, Truncated);
  53. RUN_TEST(RawPrinter, ExactlyFilled);
  54. printf("PASS\n");
  55. return 0; // 0 means success
  56. }