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

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

Issue 9384027: Add multi-byte ByteArray access methods to the Dart API. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: minor changes to formatting and comments Created 8 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « runtime/vm/dart_api_impl.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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 "include/dart_api.h" 5 #include "include/dart_api.h"
6 #include "platform/assert.h" 6 #include "platform/assert.h"
7 #include "platform/utils.h" 7 #include "platform/utils.h"
8 #include "vm/dart_api_impl.h" 8 #include "vm/dart_api_impl.h"
9 #include "vm/dart_api_state.h" 9 #include "vm/dart_api_state.h"
10 #include "vm/thread.h" 10 #include "vm/thread.h"
(...skipping 733 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 EXPECT(Dart_IsByteArray(byte_array1)); 744 EXPECT(Dart_IsByteArray(byte_array1));
745 EXPECT(Dart_IsList(byte_array1)); 745 EXPECT(Dart_IsList(byte_array1));
746 746
747 intptr_t length = 0; 747 intptr_t length = 0;
748 Dart_Handle result = Dart_ListLength(byte_array1, &length); 748 Dart_Handle result = Dart_ListLength(byte_array1, &length);
749 EXPECT_VALID(result); 749 EXPECT_VALID(result);
750 EXPECT_EQ(10, length); 750 EXPECT_EQ(10, length);
751 751
752 result = Dart_ListSetAt(byte_array1, -1, Dart_NewInteger(1)); 752 result = Dart_ListSetAt(byte_array1, -1, Dart_NewInteger(1));
753 EXPECT(Dart_IsError(result)); 753 EXPECT(Dart_IsError(result));
754 result = Dart_ByteArraySetUint8At(byte_array1, -1, 1);
755 EXPECT(Dart_IsError(result));
756
754 result = Dart_ListSetAt(byte_array1, 10, Dart_NewInteger(1)); 757 result = Dart_ListSetAt(byte_array1, 10, Dart_NewInteger(1));
755 EXPECT(Dart_IsError(result)); 758 EXPECT(Dart_IsError(result));
759 result = Dart_ByteArraySetUint8At(byte_array1, 10, 1);
760 EXPECT(Dart_IsError(result));
756 761
762 // Set through the List API.
757 for (intptr_t i = 0; i < 10; ++i) { 763 for (intptr_t i = 0; i < 10; ++i) {
758 result = Dart_ListSetAt(byte_array1, i, Dart_NewInteger(i + 1)); 764 EXPECT_VALID(Dart_ListSetAt(byte_array1, i, Dart_NewInteger(i + 1)));
759 EXPECT_VALID(result); 765 }
766 for (intptr_t i = 0; i < 10; ++i) {
767 // Get through the List API.
768 Dart_Handle integer_obj = Dart_ListGetAt(byte_array1, i);
769 EXPECT_VALID(integer_obj);
770 int64_t int64_t_value = -1;
771 EXPECT_VALID(Dart_IntegerToInt64(integer_obj, &int64_t_value));
772 EXPECT_EQ(i + 1, int64_t_value);
773 // Get through the ByteArray API.
774 uint8_t uint8_t_value = 0xFF;
775 EXPECT_VALID(Dart_ByteArrayGetUint8At(byte_array1, i, &uint8_t_value));
776 EXPECT_EQ(i + 1, uint8_t_value);
760 } 777 }
761 778
779 // Set through the ByteArray API.
762 for (intptr_t i = 0; i < 10; ++i) { 780 for (intptr_t i = 0; i < 10; ++i) {
763 result = Dart_ListGetAt(byte_array1, i); 781 EXPECT_VALID(Dart_ByteArraySetUint8At(byte_array1, i, i + 2));
764 EXPECT_VALID(result); 782 }
765 int64_t value = 0; 783 for (intptr_t i = 0; i < 10; ++i) {
766 result = Dart_IntegerToInt64(result, &value); 784 // Get through the List API.
767 EXPECT_VALID(result); 785 Dart_Handle integer_obj = Dart_ListGetAt(byte_array1, i);
768 EXPECT_EQ(i + 1, value); 786 EXPECT_VALID(integer_obj);
787 int64_t int64_t_value = -1;
788 EXPECT_VALID(Dart_IntegerToInt64(integer_obj, &int64_t_value));
789 EXPECT_EQ(i + 2, int64_t_value);
790 // Get through the ByteArray API.
791 uint8_t uint8_t_value = 0xFF;
792 EXPECT_VALID(Dart_ByteArrayGetUint8At(byte_array1, i, &uint8_t_value));
793 EXPECT_EQ(i + 2, uint8_t_value);
769 } 794 }
770 795
771 Dart_Handle byte_array2 = Dart_NewByteArray(10); 796 Dart_Handle byte_array2 = Dart_NewByteArray(10);
772 bool is_equal = false; 797 bool is_equal = false;
773 Dart_ObjectEquals(byte_array1, byte_array2, &is_equal); 798 Dart_ObjectEquals(byte_array1, byte_array2, &is_equal);
774 EXPECT(!is_equal); 799 EXPECT(!is_equal);
775 800
801 // Set through the List API.
776 for (intptr_t i = 0; i < 10; ++i) { 802 for (intptr_t i = 0; i < 10; ++i) {
777 result = Dart_ListSetAt(byte_array2, i, Dart_NewInteger(i + 1)); 803 result = Dart_ListSetAt(byte_array2, i, Dart_NewInteger(i + 2));
778 EXPECT_VALID(result); 804 EXPECT_VALID(result);
779 } 805 }
780 is_equal = false;
781 Dart_ObjectEquals(byte_array1, byte_array2, &is_equal);
782 EXPECT(!is_equal);
783
784 for (intptr_t i = 0; i < 10; ++i) { 806 for (intptr_t i = 0; i < 10; ++i) {
807 // Get through the List API.
785 Dart_Handle e1 = Dart_ListGetAt(byte_array1, i); 808 Dart_Handle e1 = Dart_ListGetAt(byte_array1, i);
786 Dart_Handle e2 = Dart_ListGetAt(byte_array2, i); 809 Dart_Handle e2 = Dart_ListGetAt(byte_array2, i);
787 is_equal = false; 810 is_equal = false;
788 Dart_ObjectEquals(e1, e2, &is_equal); 811 Dart_ObjectEquals(e1, e2, &is_equal);
789 EXPECT(is_equal); 812 EXPECT(is_equal);
813 // Get through the ByteArray API.
814 uint8_t v1 = 0xFF;
815 uint8_t v2 = 0XFF;
816 EXPECT_VALID(Dart_ByteArrayGetUint8At(byte_array1, i, &v1));
817 EXPECT_VALID(Dart_ByteArrayGetUint8At(byte_array2, i, &v2));
818 EXPECT_NE(v1, 0xFF);
819 EXPECT_NE(v2, 0xFF);
820 EXPECT_EQ(v1, v2);
821 }
822
823 byte_array2 = Dart_NewByteArray(10);
824 is_equal = false;
825 Dart_ObjectEquals(byte_array1, byte_array2, &is_equal);
826 EXPECT(!is_equal);
827
828 // Set through the ByteArray API.
829 for (intptr_t i = 0; i < 10; ++i) {
830 result = Dart_ByteArraySetUint8At(byte_array2, i, i + 2);
831 EXPECT_VALID(result);
832 }
833 for (intptr_t i = 0; i < 10; ++i) {
834 // Get through the List API.
835 Dart_Handle e1 = Dart_ListGetAt(byte_array1, i);
836 Dart_Handle e2 = Dart_ListGetAt(byte_array2, i);
837 is_equal = false;
838 Dart_ObjectEquals(e1, e2, &is_equal);
839 EXPECT(is_equal);
840 // Get through the ByteArray API.
841 uint8_t v1 = 0xFF;
842 uint8_t v2 = 0XFF;
843 EXPECT_VALID(Dart_ByteArrayGetUint8At(byte_array1, i, &v1));
844 EXPECT_VALID(Dart_ByteArrayGetUint8At(byte_array2, i, &v2));
845 EXPECT_NE(v1, 0xFF);
846 EXPECT_NE(v2, 0xFF);
847 EXPECT_EQ(v1, v2);
790 } 848 }
791 849
792 uint8_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 850 uint8_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
793 result = Dart_ListSetAsBytes(byte_array1, 0, data, 10); 851 result = Dart_ListSetAsBytes(byte_array1, 0, data, 10);
794 EXPECT_VALID(result); 852 EXPECT_VALID(result);
795 for (intptr_t i = 0; i < 10; ++i) { 853 for (intptr_t i = 0; i < 10; ++i) {
796 Dart_Handle e = Dart_ListGetAt(byte_array1, i); 854 Dart_Handle integer_obj = Dart_ListGetAt(byte_array1, i);
797 int64_t value; 855 EXPECT_VALID(integer_obj);
798 result = Dart_IntegerToInt64(e, &value); 856 int64_t int64_t_value = -1;
799 EXPECT_VALID(result); 857 EXPECT_VALID(Dart_IntegerToInt64(integer_obj, &int64_t_value));
800 EXPECT_EQ(i, value); 858 EXPECT_EQ(i, int64_t_value);
859 uint8_t uint8_t_value = 0xFF;
860 EXPECT_VALID(Dart_ByteArrayGetUint8At(byte_array1, i, &uint8_t_value));
861 EXPECT_EQ(i, uint8_t_value);
801 } 862 }
802 863
803 for (intptr_t i = 0; i < 10; ++i) { 864 for (intptr_t i = 0; i < 10; ++i) {
804 EXPECT_VALID(Dart_ListSetAt(byte_array1, i, Dart_NewInteger(10 - i))); 865 EXPECT_VALID(Dart_ListSetAt(byte_array1, i, Dart_NewInteger(10 - i)));
805 } 866 }
806 Dart_ListGetAsBytes(byte_array1, 0, data, 10); 867 Dart_ListGetAsBytes(byte_array1, 0, data, 10);
807 for (intptr_t i = 0; i < 10; ++i) { 868 for (intptr_t i = 0; i < 10; ++i) {
808 Dart_Handle e = Dart_ListGetAt(byte_array1, i); 869 Dart_Handle integer_obj = Dart_ListGetAt(byte_array1, i);
809 EXPECT_VALID(e); 870 EXPECT_VALID(integer_obj);
810 int64_t value; 871 int64_t int64_t_value = -1;
811 result = Dart_IntegerToInt64(e, &value); 872 EXPECT_VALID(Dart_IntegerToInt64(integer_obj, &int64_t_value));
812 EXPECT_VALID(result); 873 EXPECT_EQ(10 - i, int64_t_value);
813 EXPECT_EQ(10 - i, value); 874 uint8_t uint8_t_value = 0xFF;
875 EXPECT_VALID(Dart_ByteArrayGetUint8At(byte_array1, i, &uint8_t_value));
876 EXPECT_EQ(10 - i, uint8_t_value);
814 } 877 }
878
879 for (intptr_t i = 0; i < 10; ++i) {
880 EXPECT_VALID(Dart_ByteArraySetUint8At(byte_array1, i, 10 + i));
881 }
882 Dart_ListGetAsBytes(byte_array1, 0, data, 10);
883 for (intptr_t i = 0; i < 10; ++i) {
884 Dart_Handle integer_obj = Dart_ListGetAt(byte_array1, i);
885 EXPECT_VALID(integer_obj);
886 int64_t int64_t_value = -1;
887 EXPECT_VALID(Dart_IntegerToInt64(integer_obj, &int64_t_value));
888 EXPECT_EQ(10 + i, int64_t_value);
889 uint8_t uint8_t_value = 0xFF;
890 EXPECT_VALID(Dart_ByteArrayGetUint8At(byte_array1, i, &uint8_t_value));
891 EXPECT_EQ(10 + i, uint8_t_value);
892 }
893 }
894
895
896 TEST_CASE(ByteArrayAlignedMultiByteAccess) {
897 intptr_t length = 16;
898 Dart_Handle byte_array = Dart_NewByteArray(length);
899 intptr_t api_length = 0;
900 EXPECT_VALID(Dart_ListLength(byte_array, &api_length));
901 EXPECT_EQ(length, api_length);
902
903 // 4-byte aligned sets.
904
905 EXPECT_VALID(Dart_ByteArraySetFloat32At(byte_array, 0, FLT_MIN));
906 EXPECT_VALID(Dart_ByteArraySetFloat32At(byte_array, 4, FLT_MAX));
907
908 float float_value = 0.0f;
909 EXPECT_VALID(Dart_ByteArrayGetFloat32At(byte_array, 0, &float_value));
910 EXPECT_EQ(FLT_MIN, float_value);
911
912 float_value = 0.0f;
913 EXPECT_VALID(Dart_ByteArrayGetFloat32At(byte_array, 4, &float_value));
914 EXPECT_EQ(FLT_MAX, float_value);
915
916 EXPECT_VALID(Dart_ByteArraySetFloat32At(byte_array, 0, 0.0f));
917 float_value = FLT_MAX;
918 EXPECT_VALID(Dart_ByteArrayGetFloat32At(byte_array, 0, &float_value));
919 EXPECT_EQ(0.0f, float_value);
920
921 EXPECT_VALID(Dart_ByteArraySetFloat32At(byte_array, 4, 1.0f));
922 float_value = FLT_MAX;
923 EXPECT_VALID(Dart_ByteArrayGetFloat32At(byte_array, 4, &float_value));
924 EXPECT_EQ(1.0f, float_value);
925
926 EXPECT_VALID(Dart_ByteArraySetFloat32At(byte_array, 0, -1.0f));
927 float_value = FLT_MAX;
928 EXPECT_VALID(Dart_ByteArrayGetFloat32At(byte_array, 0, &float_value));
929 EXPECT_EQ(-1.0f, float_value);
930
931 // 8-byte aligned sets.
932
933 EXPECT_VALID(Dart_ByteArraySetFloat64At(byte_array, 0, DBL_MIN));
934 EXPECT_VALID(Dart_ByteArraySetFloat64At(byte_array, 8, DBL_MAX));
935
936 double double_value = 0.0;
937 EXPECT_VALID(Dart_ByteArrayGetFloat64At(byte_array, 0, &double_value));
938 EXPECT_EQ(DBL_MIN, double_value);
939
940 double_value = 0.0;
941 EXPECT_VALID(Dart_ByteArrayGetFloat64At(byte_array, 8, &double_value));
942 EXPECT_EQ(DBL_MAX, double_value);
943
944 EXPECT_VALID(Dart_ByteArraySetFloat64At(byte_array, 0, 0.0));
945 double_value = DBL_MAX;
946 EXPECT_VALID(Dart_ByteArrayGetFloat64At(byte_array, 0, &double_value));
947 EXPECT_EQ(0.0, double_value);
948
949 EXPECT_VALID(Dart_ByteArraySetFloat64At(byte_array, 8, 1.0));
950 double_value = DBL_MAX;
951 EXPECT_VALID(Dart_ByteArrayGetFloat64At(byte_array, 8, &double_value));
952 EXPECT_EQ(1.0, double_value);
953 }
954
955
956 TEST_CASE(ByteArrayMisalignedMultiByteAccess) {
957 intptr_t length = 17;
958 Dart_Handle byte_array = Dart_NewByteArray(length);
959 intptr_t api_length = 0;
960 EXPECT_VALID(Dart_ListLength(byte_array, &api_length));
961 EXPECT_EQ(length, api_length);
962
963 // 4-byte misaligned sets.
964
965 EXPECT_VALID(Dart_ByteArraySetFloat32At(byte_array, 1, FLT_MIN));
966 EXPECT_VALID(Dart_ByteArraySetFloat32At(byte_array, 5, FLT_MAX));
967
968 float float_value = 0.0f;
969 EXPECT_VALID(Dart_ByteArrayGetFloat32At(byte_array, 1, &float_value));
970 EXPECT_EQ(FLT_MIN, float_value);
971
972 float_value = 0.0f;
973 EXPECT_VALID(Dart_ByteArrayGetFloat32At(byte_array, 5, &float_value));
974 EXPECT_EQ(FLT_MAX, float_value);
975
976 EXPECT_VALID(Dart_ByteArraySetFloat32At(byte_array, 1, 0.0f));
977 float_value = FLT_MAX;
978 EXPECT_VALID(Dart_ByteArrayGetFloat32At(byte_array, 1, &float_value));
979 EXPECT_EQ(0.0f, float_value);
980
981 EXPECT_VALID(Dart_ByteArraySetFloat32At(byte_array, 5, -0.0f));
982 float_value = FLT_MAX;
983 EXPECT_VALID(Dart_ByteArrayGetFloat32At(byte_array, 5, &float_value));
984 EXPECT_EQ(-0.0f, float_value);
985
986 EXPECT_VALID(Dart_ByteArraySetFloat32At(byte_array, 5, 1.0f));
987 float_value = FLT_MAX;
988 EXPECT_VALID(Dart_ByteArrayGetFloat32At(byte_array, 5, &float_value));
989 EXPECT_EQ(1.0f, float_value);
990
991 EXPECT_VALID(Dart_ByteArraySetFloat32At(byte_array, 1, -1.0f));
992 float_value = FLT_MAX;
993 EXPECT_VALID(Dart_ByteArrayGetFloat32At(byte_array, 1, &float_value));
994 EXPECT_EQ(-1.0f, float_value);
995
996 // 8-byte misaligned sets.
997
998 EXPECT_VALID(Dart_ByteArraySetFloat64At(byte_array, 1, DBL_MIN));
999 EXPECT_VALID(Dart_ByteArraySetFloat64At(byte_array, 9, DBL_MAX));
1000
1001 double double_value = 0.0;
1002 EXPECT_VALID(Dart_ByteArrayGetFloat64At(byte_array, 1, &double_value));
1003 EXPECT_EQ(DBL_MIN, double_value);
1004
1005 double_value = 0.0;
1006 EXPECT_VALID(Dart_ByteArrayGetFloat64At(byte_array, 9, &double_value));
1007 EXPECT_EQ(DBL_MAX, double_value);
1008
1009 EXPECT_VALID(Dart_ByteArraySetFloat64At(byte_array, 1, 0.0));
1010 double_value = DBL_MAX;
1011 EXPECT_VALID(Dart_ByteArrayGetFloat64At(byte_array, 1, &double_value));
1012 EXPECT_EQ(0.0, double_value);
1013
1014 EXPECT_VALID(Dart_ByteArraySetFloat64At(byte_array, 9, -0.0));
1015 double_value = DBL_MAX;
1016 EXPECT_VALID(Dart_ByteArrayGetFloat64At(byte_array, 9, &double_value));
1017 EXPECT_EQ(-0.0, double_value);
1018
1019 EXPECT_VALID(Dart_ByteArraySetFloat64At(byte_array, 9, 1.0));
1020 double_value = DBL_MAX;
1021 EXPECT_VALID(Dart_ByteArrayGetFloat64At(byte_array, 9, &double_value));
1022 EXPECT_EQ(1.0, double_value);
1023
1024 EXPECT_VALID(Dart_ByteArraySetFloat64At(byte_array, 1, -1.0));
1025 double_value = DBL_MAX;
1026 EXPECT_VALID(Dart_ByteArrayGetFloat64At(byte_array, 1, &double_value));
1027 EXPECT_EQ(-1.0, double_value);
815 } 1028 }
816 1029
817 1030
818 TEST_CASE(ExternalByteArrayAccess) { 1031 TEST_CASE(ExternalByteArrayAccess) {
819 uint8_t data[] = { 0, 11, 22, 33, 44, 55, 66, 77 }; 1032 uint8_t data[] = { 0, 11, 22, 33, 44, 55, 66, 77 };
820 intptr_t data_length = ARRAY_SIZE(data); 1033 intptr_t data_length = ARRAY_SIZE(data);
821 1034
822 Dart_Handle obj = Dart_NewExternalByteArray(data, data_length, NULL, NULL); 1035 Dart_Handle obj = Dart_NewExternalByteArray(data, data_length, NULL, NULL);
823 EXPECT_VALID(obj); 1036 EXPECT_VALID(obj);
824 EXPECT(Dart_IsByteArray(obj)); 1037 EXPECT(Dart_IsByteArray(obj));
(...skipping 2552 matching lines...) Expand 10 before | Expand all | Expand 10 after
3377 // We should have received the expected number of interrupts. 3590 // We should have received the expected number of interrupts.
3378 EXPECT_EQ(kInterruptCount, interrupt_count); 3591 EXPECT_EQ(kInterruptCount, interrupt_count);
3379 3592
3380 // Give the spawned thread enough time to properly exit. 3593 // Give the spawned thread enough time to properly exit.
3381 Isolate::SetInterruptCallback(saved); 3594 Isolate::SetInterruptCallback(saved);
3382 } 3595 }
3383 3596
3384 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 3597 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
3385 3598
3386 } // namespace dart 3599 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/dart_api_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698