Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(560)

Side by Side Diff: runtime/vm/unicode_test.cc

Issue 1309373005: Allow individual unicode surrogate strings (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Also fix unicode tests Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/vm/unicode.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/globals.h" 5 #include "vm/globals.h"
6 #include "vm/unicode.h" 6 #include "vm/unicode.h"
7 #include "vm/unit_test.h" 7 #include "vm/unit_test.h"
8 8
9 namespace dart { 9 namespace dart {
10 10
(...skipping 814 matching lines...) Expand 10 before | Expand all | Expand 10 after
825 const char* src = "\xFC\x80\x80\x80\x80\x80"; 825 const char* src = "\xFC\x80\x80\x80\x80\x80";
826 int32_t expected[] = { 0x0 }; 826 int32_t expected[] = { 0x0 };
827 int32_t dst[ARRAY_SIZE(expected)]; 827 int32_t dst[ARRAY_SIZE(expected)];
828 memset(dst, 0xFF, sizeof(dst)); 828 memset(dst, 0xFF, sizeof(dst));
829 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 829 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
830 EXPECT(!is_valid); 830 EXPECT(!is_valid);
831 EXPECT(memcmp(expected, dst, sizeof(expected))); 831 EXPECT(memcmp(expected, dst, sizeof(expected)));
832 } 832 }
833 833
834 // 5.1 - Single UTF-16 surrogates 834 // 5.1 - Single UTF-16 surrogates
835 // UTF-8 suggests single surrogates are invalid, but both JS and
836 // Dart allow them and make use of them.
835 837
836 // 5.1.1 - U+D800 = ed a0 80 = "\xED\xA0\x80" 838 // 5.1.1 - U+D800 = ed a0 80 = "\xED\xA0\x80"
837 { 839 {
838 const char* src = "\xED\xA0\x80"; 840 const char* src = "\xED\xA0\x80";
839 int32_t expected[] = { 0xD800 }; 841 int32_t expected[] = { 0xD800 };
840 int32_t dst[ARRAY_SIZE(expected)]; 842 int32_t dst[ARRAY_SIZE(expected)];
841 memset(dst, 0, sizeof(dst)); 843 memset(dst, 0, sizeof(dst));
842 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 844 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
843 EXPECT(!is_valid); 845 EXPECT(is_valid);
844 EXPECT(memcmp(expected, dst, sizeof(expected))); 846 EXPECT(!memcmp(expected, dst, sizeof(expected)));
845 } 847 }
846 848
847 // 5.1.2 - U+DB7F = ed ad bf = "\xED\xAD\xBF" 849 // 5.1.2 - U+DB7F = ed ad bf = "\xED\xAD\xBF"
848 { 850 {
849 const char* src = "\xED\xAD\xBF"; 851 const char* src = "\xED\xAD\xBF";
850 int32_t expected[] = { 0xDB7F }; 852 int32_t expected[] = { 0xDB7F };
851 int32_t dst[ARRAY_SIZE(expected)]; 853 int32_t dst[ARRAY_SIZE(expected)];
852 memset(dst, 0, sizeof(dst)); 854 memset(dst, 0, sizeof(dst));
853 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 855 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
854 EXPECT(!is_valid); 856 EXPECT(is_valid);
855 EXPECT(memcmp(expected, dst, sizeof(expected))); 857 EXPECT(!memcmp(expected, dst, sizeof(expected)));
856 } 858 }
857 859
858 // 5.1.3 - U+DB80 = ed ae 80 = "\xED\xAE\x80" 860 // 5.1.3 - U+DB80 = ed ae 80 = "\xED\xAE\x80"
859 { 861 {
860 const char* src = "\xED\xAE\x80"; 862 const char* src = "\xED\xAE\x80";
861 int32_t expected[] = { 0xDB80 }; 863 int32_t expected[] = { 0xDB80 };
862 int32_t dst[ARRAY_SIZE(expected)]; 864 int32_t dst[ARRAY_SIZE(expected)];
863 memset(dst, 0, sizeof(dst)); 865 memset(dst, 0, sizeof(dst));
864 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 866 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
865 EXPECT(!is_valid); 867 EXPECT(is_valid);
866 EXPECT(memcmp(expected, dst, sizeof(expected))); 868 EXPECT(!memcmp(expected, dst, sizeof(expected)));
867 } 869 }
868 870
869 // 5.1.4 - U+DBFF = ed af bf = "\xED\xAF\xBF" 871 // 5.1.4 - U+DBFF = ed af bf = "\xED\xAF\xBF"
870 { 872 {
871 const char* src = "\xED\xAF\xBF"; 873 const char* src = "\xED\xAF\xBF";
872 int32_t expected[] = { 0xDBFF }; 874 int32_t expected[] = { 0xDBFF };
873 int32_t dst[ARRAY_SIZE(expected)]; 875 int32_t dst[ARRAY_SIZE(expected)];
874 memset(dst, 0, sizeof(dst)); 876 memset(dst, 0, sizeof(dst));
875 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 877 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
876 EXPECT(!is_valid); 878 EXPECT(is_valid);
877 EXPECT(memcmp(expected, dst, sizeof(expected))); 879 EXPECT(!memcmp(expected, dst, sizeof(expected)));
878 } 880 }
879 881
880 // 5.1.5 - U+DC00 = ed b0 80 = "\xED\xB0\x80" 882 // 5.1.5 - U+DC00 = ed b0 80 = "\xED\xB0\x80"
881 { 883 {
882 const char* src = "\xED\xB0\x80"; 884 const char* src = "\xED\xB0\x80";
883 int32_t expected[] = { 0xDC00 }; 885 int32_t expected[] = { 0xDC00 };
884 int32_t dst[ARRAY_SIZE(expected)]; 886 int32_t dst[ARRAY_SIZE(expected)];
885 memset(dst, 0, sizeof(dst)); 887 memset(dst, 0, sizeof(dst));
886 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 888 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
887 EXPECT(!is_valid); 889 EXPECT(is_valid);
888 EXPECT(memcmp(expected, dst, sizeof(expected))); 890 EXPECT(!memcmp(expected, dst, sizeof(expected)));
889 } 891 }
890 892
891 // 5.1.6 - U+DF80 = ed be 80 = "\xED\xBE\x80" 893 // 5.1.6 - U+DF80 = ed be 80 = "\xED\xBE\x80"
892 { 894 {
893 const char* src = "\xED\xBE\x80"; 895 const char* src = "\xED\xBE\x80";
894 int32_t expected[] = { 0xDF80 }; 896 int32_t expected[] = { 0xDF80 };
895 int32_t dst[ARRAY_SIZE(expected)]; 897 int32_t dst[ARRAY_SIZE(expected)];
896 memset(dst, 0, sizeof(dst)); 898 memset(dst, 0, sizeof(dst));
897 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 899 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
898 EXPECT(!is_valid); 900 EXPECT(is_valid);
899 EXPECT(memcmp(expected, dst, sizeof(expected))); 901 EXPECT(!memcmp(expected, dst, sizeof(expected)));
900 } 902 }
901 903
902 // 5.1.7 - U+DFFF = ed bf bf = "\xED\xBF\xBF" 904 // 5.1.7 - U+DFFF = ed bf bf = "\xED\xBF\xBF"
903 { 905 {
904 const char* src = "\xED\xBF\xBF"; 906 const char* src = "\xED\xBF\xBF";
905 int32_t expected[] = { 0xDFFF }; 907 int32_t expected[] = { 0xDFFF };
906 int32_t dst[ARRAY_SIZE(expected)]; 908 int32_t dst[ARRAY_SIZE(expected)];
907 memset(dst, 0, sizeof(dst)); 909 memset(dst, 0, sizeof(dst));
908 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 910 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
909 EXPECT(!is_valid); 911 EXPECT(is_valid);
910 EXPECT(memcmp(expected, dst, sizeof(expected))); 912 EXPECT(!memcmp(expected, dst, sizeof(expected)));
911 } 913 }
912 914
913 // 5.2 Paired UTF-16 surrogates 915 // 5.2 Paired UTF-16 surrogates
916 // Also not a valid string, but accepted in Dart, even if it doesn't make
917 // sense. e.g.
918 // var s = new String.fromCharCodes([0xd800, 0xDC00]);
919 // print(s.runes); // (65536) (0x10000)
920 // print(s.codeUnits); // [55296, 56320]
914 921
915 // 5.2.1 - U+D800 U+DC00 = ed a0 80 ed b0 80 = "\xED\xA0\x80\xED\xB0\x80" 922 // 5.2.1 - U+D800 U+DC00 = ed a0 80 ed b0 80 = "\xED\xA0\x80\xED\xB0\x80"
916 { 923 {
917 const char* src = "\xED\xA0\x80\xED\xB0\x80"; 924 const char* src = "\xED\xA0\x80\xED\xB0\x80";
918 int32_t expected[] = { 0xD800, 0xDC00 }; 925 int32_t expected[] = { 0xD800, 0xDC00 };
919 int32_t dst[ARRAY_SIZE(expected)]; 926 int32_t dst[ARRAY_SIZE(expected)];
920 memset(dst, 0, sizeof(dst)); 927 memset(dst, 0, sizeof(dst));
921 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 928 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
922 EXPECT(!is_valid); 929 EXPECT(is_valid);
923 EXPECT(memcmp(expected, dst, sizeof(expected))); 930 EXPECT(!memcmp(expected, dst, sizeof(expected)));
924 } 931 }
925 932
926 // 5.2.2 - U+D800 U+DFFF = ed a0 80 ed bf bf = "\xED\xA0\x80\xED\xBF\xBF" 933 // 5.2.2 - U+D800 U+DFFF = ed a0 80 ed bf bf = "\xED\xA0\x80\xED\xBF\xBF"
927 { 934 {
928 const char* src = "\xED\xA0\x80\xED\xBF\xBF"; 935 const char* src = "\xED\xA0\x80\xED\xBF\xBF";
929 int32_t expected[] = { 0xD800, 0xDFFF }; 936 int32_t expected[] = { 0xD800, 0xDFFF };
930 int32_t dst[ARRAY_SIZE(expected)]; 937 int32_t dst[ARRAY_SIZE(expected)];
931 memset(dst, 0, sizeof(dst)); 938 memset(dst, 0, sizeof(dst));
932 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 939 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
933 EXPECT(!is_valid); 940 EXPECT(is_valid);
934 EXPECT(memcmp(expected, dst, sizeof(expected))); 941 EXPECT(!memcmp(expected, dst, sizeof(expected)));
935 } 942 }
936 943
937 // 5.2.3 - U+DB7F U+DC00 = ed a0 80 ed bf bf = "\xED\xAD\xBF\xED\xB0\x80" 944 // 5.2.3 - U+DB7F U+DC00 = ed a0 80 ed bf bf = "\xED\xAD\xBF\xED\xB0\x80"
938 { 945 {
939 const char* src = "\xED\xAD\xBF\xED\xB0\x80"; 946 const char* src = "\xED\xAD\xBF\xED\xB0\x80";
940 int32_t expected[] = { 0xDB7F, 0xDC00 }; 947 int32_t expected[] = { 0xDB7F, 0xDC00 };
941 int32_t dst[ARRAY_SIZE(expected)]; 948 int32_t dst[ARRAY_SIZE(expected)];
942 memset(dst, 0, sizeof(dst)); 949 memset(dst, 0, sizeof(dst));
943 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 950 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
944 EXPECT(!is_valid); 951 EXPECT(is_valid);
945 EXPECT(memcmp(expected, dst, sizeof(expected))); 952 EXPECT(!memcmp(expected, dst, sizeof(expected)));
946 } 953 }
947 954
948 // 5.2.4 - U+DB7F U+DFFF = ed ad bf ed bf bf = "\xED\xAD\xBF\xED\xBF\xBF" 955 // 5.2.4 - U+DB7F U+DFFF = ed ad bf ed bf bf = "\xED\xAD\xBF\xED\xBF\xBF"
949 { 956 {
950 const char* src = "\xED\xAD\xBF\xED\xBF\xBF"; 957 const char* src = "\xED\xAD\xBF\xED\xBF\xBF";
951 int32_t expected[] = { 0xDB7F, 0xDFFF }; 958 int32_t expected[] = { 0xDB7F, 0xDFFF };
952 int32_t dst[ARRAY_SIZE(expected)]; 959 int32_t dst[ARRAY_SIZE(expected)];
953 memset(dst, 0, sizeof(dst)); 960 memset(dst, 0, sizeof(dst));
954 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 961 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
955 EXPECT(!is_valid); 962 EXPECT(is_valid);
956 EXPECT(memcmp(expected, dst, sizeof(expected))); 963 EXPECT(!memcmp(expected, dst, sizeof(expected)));
957 } 964 }
958 965
959 // 5.2.5 - U+DB80 U+DC00 = ed ae 80 ed b0 80 = "\xED\xAE\x80\xED\xB0\x80" 966 // 5.2.5 - U+DB80 U+DC00 = ed ae 80 ed b0 80 = "\xED\xAE\x80\xED\xB0\x80"
960 { 967 {
961 const char* src = "\xED\xAE\x80\xED\xB0\x80"; 968 const char* src = "\xED\xAE\x80\xED\xB0\x80";
962 int32_t expected[] = { 0xDB80, 0xDC00 }; 969 int32_t expected[] = { 0xDB80, 0xDC00 };
963 int32_t dst[ARRAY_SIZE(expected)]; 970 int32_t dst[ARRAY_SIZE(expected)];
964 memset(dst, 0, sizeof(dst)); 971 memset(dst, 0, sizeof(dst));
965 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 972 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
966 EXPECT(!is_valid); 973 EXPECT(is_valid);
967 EXPECT(memcmp(expected, dst, sizeof(expected))); 974 EXPECT(!memcmp(expected, dst, sizeof(expected)));
968 } 975 }
969 976
970 // 5.2.6 - U+DB80 U+DFFF = ed ae 80 ed bf bf = "\xED\xAE\x80\xED\xBF\xBF" 977 // 5.2.6 - U+DB80 U+DFFF = ed ae 80 ed bf bf = "\xED\xAE\x80\xED\xBF\xBF"
971 { 978 {
972 const char* src = "\xED\xAE\x80\xED\xBF\xBF"; 979 const char* src = "\xED\xAE\x80\xED\xBF\xBF";
973 int32_t expected[] = { 0xDB80, 0xDFFF }; 980 int32_t expected[] = { 0xDB80, 0xDFFF };
974 int32_t dst[ARRAY_SIZE(expected)]; 981 int32_t dst[ARRAY_SIZE(expected)];
975 memset(dst, 0, sizeof(dst)); 982 memset(dst, 0, sizeof(dst));
976 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 983 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
977 EXPECT(!is_valid); 984 EXPECT(is_valid);
978 EXPECT(memcmp(expected, dst, sizeof(expected))); 985 EXPECT(!memcmp(expected, dst, sizeof(expected)));
979 } 986 }
980 987
981 // 5.2.7 - U+DBFF U+DC00 = ed af bf ed b0 80 = "\xED\xAF\xBF\xED\xB0\x80" 988 // 5.2.7 - U+DBFF U+DC00 = ed af bf ed b0 80 = "\xED\xAF\xBF\xED\xB0\x80"
982 { 989 {
983 const char* src = "\xED\xAF\xBF\xED\xB0\x80"; 990 const char* src = "\xED\xAF\xBF\xED\xB0\x80";
984 int32_t expected[] = { 0xDBFF, 0xDC00 }; 991 int32_t expected[] = { 0xDBFF, 0xDC00 };
985 int32_t dst[ARRAY_SIZE(expected)]; 992 int32_t dst[ARRAY_SIZE(expected)];
986 memset(dst, 0, sizeof(dst)); 993 memset(dst, 0, sizeof(dst));
987 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 994 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
988 EXPECT(!is_valid); 995 EXPECT(is_valid);
989 EXPECT(memcmp(expected, dst, sizeof(expected))); 996 EXPECT(!memcmp(expected, dst, sizeof(expected)));
990 } 997 }
991 998
992 // 5.2.8 - U+DBFF U+DFFF = ed af bf ed bf bf = "\xED\xAF\xBF\xED\xBF\xBF" 999 // 5.2.8 - U+DBFF U+DFFF = ed af bf ed bf bf = "\xED\xAF\xBF\xED\xBF\xBF"
993 { 1000 {
994 const char* src = "\xED\xAF\xBF\xED\xBF\xBF"; 1001 const char* src = "\xED\xAF\xBF\xED\xBF\xBF";
995 int32_t expected[] = { 0xDBFF, 0xDFFF }; 1002 int32_t expected[] = { 0xDBFF, 0xDFFF };
996 int32_t dst[ARRAY_SIZE(expected)]; 1003 int32_t dst[ARRAY_SIZE(expected)];
997 memset(dst, 0, sizeof(dst)); 1004 memset(dst, 0, sizeof(dst));
998 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 1005 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
999 EXPECT(!is_valid); 1006 EXPECT(is_valid);
1000 EXPECT(memcmp(expected, dst, sizeof(expected))); 1007 EXPECT(!memcmp(expected, dst, sizeof(expected)));
1001 } 1008 }
1002 1009
1003 // 5.3 - Other illegal code positions 1010 // 5.3 - Other illegal code positions
1004 1011
1005 // 5.3.1 - U+FFFE = ef bf be = "\xEF\xBF\xBE" 1012 // 5.3.1 - U+FFFE = ef bf be = "\xEF\xBF\xBE"
1006 { 1013 {
1007 const char* src = "\xEF\xBF\xBE"; 1014 const char* src = "\xEF\xBF\xBE";
1008 int32_t expected[] = { 0xFFFE }; 1015 int32_t expected[] = { 0xFFFE };
1009 int32_t dst[ARRAY_SIZE(expected)]; 1016 int32_t dst[ARRAY_SIZE(expected)];
1010 memset(dst, 0, sizeof(dst)); 1017 memset(dst, 0, sizeof(dst));
1011 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 1018 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
1012 EXPECT(is_valid); 1019 EXPECT(is_valid);
1013 EXPECT(!memcmp(expected, dst, sizeof(expected))); 1020 EXPECT(!memcmp(expected, dst, sizeof(expected)));
1014 } 1021 }
1015 1022
1016 // 5.3.2 - U+FFFF = ef bf bf = "\xEF\xBF\xBF" 1023 // 5.3.2 - U+FFFF = ef bf bf = "\xEF\xBF\xBF"
1017 { 1024 {
1018 const char* src = "\xEF\xBF\xBF"; 1025 const char* src = "\xEF\xBF\xBF";
1019 int32_t expected[] = { 0xFFFF }; 1026 int32_t expected[] = { 0xFFFF };
1020 int32_t dst[ARRAY_SIZE(expected)]; 1027 int32_t dst[ARRAY_SIZE(expected)];
1021 memset(dst, 0, sizeof(dst)); 1028 memset(dst, 0, sizeof(dst));
1022 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst)); 1029 bool is_valid = Utf8::DecodeCStringToUTF32(src, dst, ARRAY_SIZE(dst));
1023 EXPECT(is_valid); 1030 EXPECT(is_valid);
1024 EXPECT(!memcmp(expected, dst, sizeof(expected))); 1031 EXPECT(!memcmp(expected, dst, sizeof(expected)));
1025 } 1032 }
1026 } 1033 }
1027 1034
1028 } // namespace dart 1035 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/unicode.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698