comparison OrthancFramework/UnitTestsSources/RestApiTests.cpp @ 4652:0ad5736c8d62

use plain C strings in MultipartStreamReader instead of std::string to allow further optimizations
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 May 2021 11:50:14 +0200
parents 017ab543e6ef
children 7053502fbf97
comparison
equal deleted inserted replaced
4651:365f51fae413 4652:0ad5736c8d62
29 29
30 #include "../Sources/ChunkedBuffer.h" 30 #include "../Sources/ChunkedBuffer.h"
31 #include "../Sources/Compression/ZlibCompressor.h" 31 #include "../Sources/Compression/ZlibCompressor.h"
32 #include "../Sources/HttpServer/HttpContentNegociation.h" 32 #include "../Sources/HttpServer/HttpContentNegociation.h"
33 #include "../Sources/HttpServer/MultipartStreamReader.h" 33 #include "../Sources/HttpServer/MultipartStreamReader.h"
34 #include "../Sources/HttpServer/StringMatcher.h"
34 #include "../Sources/Logging.h" 35 #include "../Sources/Logging.h"
35 #include "../Sources/OrthancException.h" 36 #include "../Sources/OrthancException.h"
36 #include "../Sources/RestApi/RestApiHierarchy.h" 37 #include "../Sources/RestApi/RestApiHierarchy.h"
37 #include "../Sources/WebServiceParameters.h" 38 #include "../Sources/WebServiceParameters.h"
38 39
720 } 721 }
721 722
722 { 723 {
723 std::string s(10u, '\0'); // String with null values 724 std::string s(10u, '\0'); // String with null values
724 ASSERT_EQ(10u, s.size()); 725 ASSERT_EQ(10u, s.size());
725 ASSERT_EQ(10u, s.size());
726 ASSERT_FALSE(matcher.Apply(s)); 726 ASSERT_FALSE(matcher.Apply(s));
727 727
728 s[9] = '-'; 728 s[9] = '-';
729 ASSERT_FALSE(matcher.Apply(s)); 729 ASSERT_FALSE(matcher.Apply(s));
730 730
738 ASSERT_EQ(s.end() - 3, matcher.GetMatchBegin()); 738 ASSERT_EQ(s.end() - 3, matcher.GetMatchBegin());
739 ASSERT_EQ(s.end(), matcher.GetMatchEnd()); 739 ASSERT_EQ(s.end(), matcher.GetMatchEnd());
740 } 740 }
741 } 741 }
742 742
743
744 TEST(CStringMatcher, Basic)
745 {
746 CStringMatcher matcher("---");
747
748 ASSERT_THROW(matcher.GetMatchBegin(), OrthancException);
749
750 {
751 ASSERT_FALSE(matcher.Apply(NULL, 0));
752
753 const std::string s = "";
754 ASSERT_FALSE(matcher.Apply(s));
755 }
756
757 {
758 const char* s = "abc---def";
759 ASSERT_TRUE(matcher.Apply(s, s + 9));
760
761 ASSERT_EQ('a', matcher.GetMatchBegin()[-3]);
762 ASSERT_EQ('b', matcher.GetMatchBegin()[-2]);
763 ASSERT_EQ('c', matcher.GetMatchBegin()[-1]);
764 ASSERT_EQ('-', matcher.GetMatchBegin()[0]);
765 ASSERT_EQ('-', matcher.GetMatchBegin()[1]);
766 ASSERT_EQ('-', matcher.GetMatchBegin()[2]);
767 ASSERT_EQ('d', matcher.GetMatchBegin()[3]);
768 ASSERT_EQ('e', matcher.GetMatchBegin()[4]);
769 ASSERT_EQ('f', matcher.GetMatchBegin()[5]);
770 ASSERT_EQ('\0', matcher.GetMatchBegin()[6]);
771
772 ASSERT_EQ('a', matcher.GetMatchEnd()[-6]);
773 ASSERT_EQ('b', matcher.GetMatchEnd()[-5]);
774 ASSERT_EQ('c', matcher.GetMatchEnd()[-4]);
775 ASSERT_EQ('-', matcher.GetMatchEnd()[-3]);
776 ASSERT_EQ('-', matcher.GetMatchEnd()[-2]);
777 ASSERT_EQ('-', matcher.GetMatchEnd()[-1]);
778 ASSERT_EQ('d', matcher.GetMatchEnd()[0]);
779 ASSERT_EQ('e', matcher.GetMatchEnd()[1]);
780 ASSERT_EQ('f', matcher.GetMatchEnd()[2]);
781 ASSERT_EQ('\0', matcher.GetMatchEnd()[3]);
782 }
783
784 {
785 const std::string s = "abc----def";
786 ASSERT_TRUE(matcher.Apply(s));
787 ASSERT_EQ(3, std::distance(s.c_str(), matcher.GetMatchBegin()));
788 ASSERT_EQ("---", std::string(matcher.GetMatchBegin(), matcher.GetMatchEnd()));
789 }
790
791 {
792 const std::string s = "abc---";
793 ASSERT_TRUE(matcher.Apply(s));
794 ASSERT_EQ(3, std::distance(s.c_str(), matcher.GetMatchBegin()));
795 ASSERT_EQ(s.c_str() + s.size(), matcher.GetMatchEnd());
796 ASSERT_EQ("---", std::string(matcher.GetMatchBegin(), matcher.GetMatchEnd()));
797 ASSERT_EQ("", std::string(matcher.GetMatchEnd(), s.c_str() + s.size()));
798 }
799
800 {
801 const std::string s = "abc--def";
802 ASSERT_FALSE(matcher.Apply(s));
803 ASSERT_THROW(matcher.GetMatchBegin(), OrthancException);
804 ASSERT_THROW(matcher.GetMatchEnd(), OrthancException);
805 }
806
807 {
808 std::string s(10u, '\0'); // String with null values
809 ASSERT_EQ(10u, s.size());
810 ASSERT_FALSE(matcher.Apply(s));
811
812 s[9] = '-';
813 ASSERT_FALSE(matcher.Apply(s));
814
815 s[8] = '-';
816 ASSERT_FALSE(matcher.Apply(s));
817
818 s[7] = '-';
819 ASSERT_TRUE(matcher.Apply(s));
820 ASSERT_EQ(s.c_str() + 7, matcher.GetMatchBegin());
821 ASSERT_EQ(s.c_str() + 10, matcher.GetMatchEnd());
822 ASSERT_EQ(s.c_str() + s.size() - 3, matcher.GetMatchBegin());
823 ASSERT_EQ(s.c_str() + s.size(), matcher.GetMatchEnd());
824 }
825 }
743 826
744 827
745 class MultipartTester : public MultipartStreamReader::IHandler 828 class MultipartTester : public MultipartStreamReader::IHandler
746 { 829 {
747 private: 830 private: