annotation_test.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 2019 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "tuningfork/annotation_util.h"
  17. #include "gtest/gtest.h"
  18. using namespace annotation_util;
  19. std::vector<int> TestSetup(const std::vector<int>& enum_sizes,
  20. const std::vector<int>& expected){
  21. std::vector<int> radix_mult;
  22. SetUpAnnotationRadixes(radix_mult, enum_sizes);
  23. EXPECT_EQ(radix_mult, expected);
  24. return radix_mult;
  25. }
  26. TEST(Annotation, Setup) {
  27. TestSetup( {}, { 1 } );
  28. TestSetup( { 1 }, { 2 } );
  29. TestSetup( { 1, 1 }, { 2, 4 } );
  30. TestSetup( { 1, 1, 1 }, { 2, 4, 8 } );
  31. TestSetup( { 2, 1, 1 }, { 3, 6, 12 } );
  32. }
  33. void CheckEncodeDecode(AnnotationId id,
  34. const std::vector<int>& radix_mult, const std::string& err) {
  35. SerializedAnnotation ser;
  36. EXPECT_EQ(SerializeAnnotationId(id, ser, radix_mult), 0) << err << ": error serializing";
  37. auto back = DecodeAnnotationSerialization( ser, radix_mult);
  38. EXPECT_EQ(id, back) << err;
  39. }
  40. TEST(Annotation, EncodeDecodeGood) {
  41. auto radix_mult = TestSetup( {2, 3, 4} , {3, 12, 60} );
  42. CheckEncodeDecode( 1, radix_mult, "First");
  43. CheckEncodeDecode( 2, radix_mult, "Second");
  44. CheckEncodeDecode( 3, radix_mult, "Third");
  45. }
  46. void CheckGood(SerializedAnnotation ser, AnnotationId id,
  47. const std::vector<int>& radix_mult) {
  48. auto back = DecodeAnnotationSerialization( ser, radix_mult);
  49. EXPECT_EQ(back, id) << "Good";
  50. }
  51. void CheckBad(SerializedAnnotation ser,
  52. const std::vector<int>& radix_mult) {
  53. auto back = DecodeAnnotationSerialization( ser, radix_mult);
  54. EXPECT_EQ(back, kAnnotationError) << "Bad";
  55. }
  56. TEST(Annotation, Decode) {
  57. auto radix_mult = TestSetup( {2, 3, 4} , {3, 12, 60} );
  58. CheckBad( {1}, radix_mult);
  59. CheckBad( {5<<3, 0 }, radix_mult);
  60. CheckBad( {1<<3, 0 }, radix_mult);
  61. CheckBad( {1<<3, 8 }, radix_mult);
  62. CheckBad( {1<<3, 1, 2<<3, 2, 3<<3}, radix_mult);
  63. CheckBad( {1<<3, 1, 2<<3, 2, 3<<3, 3, 0}, radix_mult);
  64. CheckGood( {1<<3, 1 }, 1, radix_mult);
  65. CheckGood( {1<<3, 2 }, 2, radix_mult);
  66. CheckGood( {1<<3, 1, 2<<3, 1}, 4, radix_mult);
  67. CheckGood( {1<<3, 2, 2<<3, 3, 3<<3, 4}, 59, radix_mult);
  68. }