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

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

Issue 10379018: Revert "Revert "Implement {Int,Uint}{8,16,32,64} and Float{32,64} typed arrays."" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 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
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 "platform/assert.h" 5 #include "platform/assert.h"
6 #include "vm/assembler.h" 6 #include "vm/assembler.h"
7 #include "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/isolate.h" 8 #include "vm/isolate.h"
9 #include "vm/object.h" 9 #include "vm/object.h"
10 #include "vm/object_store.h" 10 #include "vm/object_store.h"
(...skipping 1836 matching lines...) Expand 10 before | Expand all | Expand 10 after
1847 new_array ^= obj.raw(); 1847 new_array ^= obj.raw();
1848 EXPECT_EQ(1, new_array.Length()); 1848 EXPECT_EQ(1, new_array.Length());
1849 addr += used_size; 1849 addr += used_size;
1850 obj = RawObject::FromAddr(addr); 1850 obj = RawObject::FromAddr(addr);
1851 EXPECT(obj.IsArray()); 1851 EXPECT(obj.IsArray());
1852 new_array ^= obj.raw(); 1852 new_array ^= obj.raw();
1853 EXPECT_EQ(2, new_array.Length()); 1853 EXPECT_EQ(2, new_array.Length());
1854 } 1854 }
1855 1855
1856 1856
1857 TEST_CASE(InternalByteArray) {
1858 uint8_t data[] = { 253, 254, 255, 0, 1, 2, 3, 4 };
1859 intptr_t data_length = ARRAY_SIZE(data);
1860
1861 const Int8Array& int8_array =
1862 Int8Array::Handle(Int8Array::New(reinterpret_cast<int8_t*>(data),
1863 data_length));
1864 EXPECT(!int8_array.IsNull());
1865 EXPECT_EQ(data_length, int8_array.Length());
1866
1867 EXPECT_EQ(-3, int8_array.At(0));
1868 uint8_t at_0;
1869 ByteArray::Copy(&at_0, int8_array, 0, sizeof(at_0));
1870 EXPECT_EQ(253, at_0);
1871
1872 EXPECT_EQ(-2, int8_array.At(1));
1873 uint8_t at_1;
1874 ByteArray::Copy(&at_1, int8_array, 1, sizeof(at_1));
1875 EXPECT_EQ(254, at_1);
1876
1877 EXPECT_EQ(-1, int8_array.At(2));
1878 uint8_t at_2;
1879 ByteArray::Copy(&at_2, int8_array, 2, sizeof(at_2));
1880 EXPECT_EQ(255, at_2);
1881
1882 EXPECT_EQ(0, int8_array.At(3));
1883 uint8_t at_3;
1884 ByteArray::Copy(&at_3, int8_array, 3, sizeof(at_3));
1885 EXPECT_EQ(0, at_3);
1886
1887 EXPECT_EQ(1, int8_array.At(4));
1888 uint8_t at_4;
1889 ByteArray::Copy(&at_4, int8_array, 4, sizeof(at_4));
1890 EXPECT_EQ(1, at_4);
1891
1892 EXPECT_EQ(2, int8_array.At(5));
1893 uint8_t at_5;
1894 ByteArray::Copy(&at_5, int8_array, 5, sizeof(at_5));
1895 EXPECT_EQ(2, at_5);
1896
1897 EXPECT_EQ(3, int8_array.At(6));
1898 uint8_t at_6;
1899 ByteArray::Copy(&at_6, int8_array, 6, sizeof(at_6));
1900 EXPECT_EQ(3, at_6);
1901
1902 EXPECT_EQ(4, int8_array.At(7));
1903 uint8_t at_7;
1904 ByteArray::Copy(&at_7, int8_array, 7, sizeof(at_7));
1905 EXPECT_EQ(4, at_7);
1906
1907 const Int8Array& int8_array2 =
1908 Int8Array::Handle(Int8Array::New(reinterpret_cast<int8_t*>(data),
1909 data_length));
1910 EXPECT(!int8_array.IsNull());
1911 EXPECT_EQ(data_length, int8_array.Length());
1912
1913 for (intptr_t i = 0; i < data_length; ++i) {
1914 EXPECT_EQ(int8_array.At(i), int8_array2.At(i));
1915 }
1916 for (intptr_t i = 0; i < data_length; ++i) {
1917 int8_array.SetAt(i, 123 + i);
1918 }
1919 for (intptr_t i = 0; i < data_length; ++i) {
1920 EXPECT(int8_array.At(i) != int8_array2.At(i));
1921 }
1922 }
1923
1924
1857 TEST_CASE(ExternalByteArray) { 1925 TEST_CASE(ExternalByteArray) {
1858 uint8_t data[] = { 253, 254, 255, 0, 1, 2, 3, 4 }; 1926 uint8_t data[] = { 253, 254, 255, 0, 1, 2, 3, 4 };
1859 intptr_t data_length = ARRAY_SIZE(data); 1927 intptr_t data_length = ARRAY_SIZE(data);
1860 1928
1861 const ExternalByteArray& array1 = 1929 const ExternalInt8Array& int8_array =
1862 ExternalByteArray::Handle(ExternalByteArray::New(data, 1930 ExternalInt8Array::Handle(
1863 data_length, 1931 ExternalInt8Array::New(reinterpret_cast<int8_t*>(data),
1864 NULL, 1932 data_length, NULL, NULL));
1865 NULL)); 1933 EXPECT(!int8_array.IsNull());
1866 EXPECT(!array1.IsNull()); 1934 EXPECT_EQ(data_length, int8_array.Length());
1867 EXPECT_EQ(data_length, array1.Length()); 1935
1868 EXPECT_EQ(-3, array1.At<int8_t>(0)); 1936 const ExternalUint8Array& uint8_array =
1869 EXPECT_EQ(253, array1.At<uint8_t>(0)); 1937 ExternalUint8Array::Handle(
1870 EXPECT_EQ(-2, array1.At<int8_t>(1)); 1938 ExternalUint8Array::New(data, data_length, NULL, NULL));
1871 EXPECT_EQ(254, array1.At<uint8_t>(1)); 1939 EXPECT(!uint8_array.IsNull());
1872 EXPECT_EQ(-1, array1.At<int8_t>(2)); 1940 EXPECT_EQ(data_length, uint8_array.Length());
1873 EXPECT_EQ(255, array1.At<uint8_t>(2)); 1941
1874 EXPECT_EQ(0, array1.At<int8_t>(3)); 1942 EXPECT_EQ(-3, int8_array.At(0));
1875 EXPECT_EQ(0, array1.At<uint8_t>(3)); 1943 EXPECT_EQ(253, uint8_array.At(0));
1876 EXPECT_EQ(1, array1.At<int8_t>(4)); 1944
1877 EXPECT_EQ(1, array1.At<uint8_t>(4)); 1945 EXPECT_EQ(-2, int8_array.At(1));
1878 EXPECT_EQ(2, array1.At<int8_t>(5)); 1946 EXPECT_EQ(254, uint8_array.At(1));
1879 EXPECT_EQ(2, array1.At<uint8_t>(5)); 1947
1880 1948 EXPECT_EQ(-1, int8_array.At(2));
1881 const ExternalByteArray& array2 = 1949 EXPECT_EQ(255, uint8_array.At(2));
1882 ExternalByteArray::Handle(ExternalByteArray::New(data, 1950
1883 data_length, 1951 EXPECT_EQ(0, int8_array.At(3));
1884 NULL, 1952 EXPECT_EQ(0, uint8_array.At(3));
1885 NULL)); 1953
1886 EXPECT(!array1.IsNull()); 1954 EXPECT_EQ(1, int8_array.At(4));
1887 EXPECT_EQ(data_length, array2.Length()); 1955 EXPECT_EQ(1, uint8_array.At(4));
1888 for (intptr_t i = 0 ; i < array1.Length(); ++i) { 1956
1889 EXPECT_EQ(array1.At<uint8_t>(i), array2.At<uint8_t>(i)); 1957 EXPECT_EQ(2, int8_array.At(5));
1890 } 1958 EXPECT_EQ(2, uint8_array.At(5));
1891 1959
1892 array1.SetAt<uint8_t>(0, 123); 1960 for (intptr_t i = 0 ; i < int8_array.Length(); ++i) {
1893 array2.SetAt<int8_t>(2, -123); 1961 uint8_t value = 0;
1894 for (intptr_t i = 0 ; i < array1.Length(); ++i) { 1962 ByteArray::Copy(&value, int8_array, i, sizeof(value));
1895 EXPECT_EQ(array1.At<uint8_t>(i), array2.At<uint8_t>(i)); 1963 EXPECT_EQ(value, uint8_array.At(i));
1896 } 1964 }
1897 } 1965
1898 1966 int8_array.SetAt(2, -123);
1899 1967 uint8_array.SetAt(0, 123);
1900 TEST_CASE(ExternalByteArrayAlignedAccess) { 1968 for (intptr_t i = 0 ; i < int8_array.Length(); ++i) {
1901 intptr_t length = 16; 1969 int8_t value = 0;
1902 uint8_t* data = new uint8_t[length]; 1970 ByteArray::Copy(&value, uint8_array, i, sizeof(value));
1903 1971 EXPECT_EQ(int8_array.At(i), value);
1904 const ExternalByteArray& array1 = 1972 }
1905 ExternalByteArray::Handle(ExternalByteArray::New(data, 1973 }
1906 length, 1974
1907 NULL, 1975
1908 NULL)); 1976 TEST_CASE(ByteArrayCopyInternal) {
1909 EXPECT(!array1.IsNull());
1910 EXPECT_EQ(length, array1.Length());
1911
1912 const ExternalByteArray& array2 =
1913 ExternalByteArray::Handle(ExternalByteArray::New(data,
1914 length,
1915 NULL,
1916 NULL));
1917 EXPECT(!array2.IsNull());
1918 EXPECT_EQ(length, array2.Length());
1919
1920 memset(data, 0xAA, ARRAY_SIZE(data));
1921 array1.SetAt<float>(0, FLT_MIN);
1922 array1.SetAt<float>(4, FLT_MAX);
1923 EXPECT_EQ(FLT_MIN, array2.At<float>(0));
1924 EXPECT_EQ(FLT_MAX, array2.At<float>(4));
1925
1926 memset(data, 0xAA, ARRAY_SIZE(data));
1927 array1.SetAt<float>(0, 0.0f);
1928 EXPECT_EQ(0.0f, array2.At<float>(0));
1929 array1.SetAt<float>(4, 1.0f);
1930 EXPECT_EQ(1.0f, array2.At<float>(4));
1931
1932 memset(data, 0xAA, ARRAY_SIZE(data));
1933 array1.SetAt<double>(0, DBL_MIN);
1934 EXPECT_EQ(DBL_MIN, array2.At<double>(0));
1935 array1.SetAt<double>(8, DBL_MAX);
1936 EXPECT_EQ(DBL_MAX, array2.At<double>(8));
1937
1938 memset(data, 0xAA, ARRAY_SIZE(data));
1939 array1.SetAt<double>(0, 0.0);
1940 EXPECT_EQ(0.0, array2.At<double>(0));
1941 array1.SetAt<double>(8, 1.0);
1942 EXPECT_EQ(1.0, array2.At<double>(8));
1943
1944 delete[] data;
1945 }
1946
1947
1948 TEST_CASE(ExternalByteArrayUnlignedAccess) {
1949 intptr_t length = 24;
1950 uint8_t* data = new uint8_t[length];
1951
1952 const ExternalByteArray& array1 =
1953 ExternalByteArray::Handle(ExternalByteArray::New(data,
1954 length,
1955 NULL,
1956 NULL));
1957 EXPECT(!array1.IsNull());
1958 EXPECT_EQ(length, array1.Length());
1959
1960 const ExternalByteArray& array2 =
1961 ExternalByteArray::Handle(ExternalByteArray::New(data,
1962 length,
1963 NULL,
1964 NULL));
1965 EXPECT(!array2.IsNull());
1966 EXPECT_EQ(length, array2.Length());
1967
1968 int float_misalign = 3;
1969 memset(data, 0xAA, ARRAY_SIZE(data));
1970 array1.SetUnalignedAt<float>(float_misalign, FLT_MIN);
1971 array1.SetUnalignedAt<float>(4 + float_misalign, FLT_MAX);
1972 EXPECT_EQ(FLT_MIN, array2.UnalignedAt<float>(float_misalign));
1973 EXPECT_EQ(FLT_MAX, array2.UnalignedAt<float>(4 + float_misalign));
1974
1975 memset(data, 0xAA, ARRAY_SIZE(data));
1976 array1.SetUnalignedAt<float>(float_misalign, 0.0f);
1977 EXPECT_EQ(0.0f, array2.UnalignedAt<float>(float_misalign));
1978 array1.SetUnalignedAt<float>(4 + float_misalign, 1.0f);
1979 EXPECT_EQ(1.0f, array2.UnalignedAt<float>(4 + float_misalign));
1980
1981 int double_misalign = 5;
1982 memset(data, 0xAA, ARRAY_SIZE(data));
1983 array1.SetUnalignedAt<double>(double_misalign, DBL_MIN);
1984 EXPECT_EQ(DBL_MIN, array2.UnalignedAt<double>(double_misalign));
1985 array1.SetUnalignedAt<double>(8 + double_misalign, DBL_MAX);
1986 EXPECT_EQ(DBL_MAX,
1987 array2.UnalignedAt<double>(8 + double_misalign));
1988
1989 memset(data, 0xAA, ARRAY_SIZE(data));
1990 array1.SetUnalignedAt<double>(double_misalign, 0.0);
1991 EXPECT_EQ(0.0, array2.UnalignedAt<double>(double_misalign));
1992 array1.SetUnalignedAt<double>(8 + double_misalign, 1.0);
1993 EXPECT_EQ(1.0, array2.UnalignedAt<double>(8 + double_misalign));
1994
1995 delete[] data;
1996 }
1997
1998
1999 TEST_CASE(ExternalByteArraySkewedUnalignedBaseAccess) {
2000 intptr_t length = 24;
2001 uint8_t* data = new uint8_t[length];
2002
2003 int skew = 2;
2004
2005 const ExternalByteArray& array1 =
2006 ExternalByteArray::Handle(ExternalByteArray::New(data + 3,
2007 length,
2008 NULL,
2009 NULL));
2010 EXPECT(!array1.IsNull());
2011 EXPECT_EQ(length, array1.Length());
2012
2013 const ExternalByteArray& array2 =
2014 ExternalByteArray::Handle(ExternalByteArray::New(data + 1,
2015 length,
2016 NULL,
2017 NULL));
2018 EXPECT(!array2.IsNull());
2019 EXPECT_EQ(length, array2.Length());
2020
2021 memset(data, 0xAA, ARRAY_SIZE(data));
2022 array1.SetUnalignedAt<float>(0, FLT_MIN);
2023 array1.SetUnalignedAt<float>(4, FLT_MAX);
2024 EXPECT_EQ(FLT_MIN, array2.UnalignedAt<float>(0 + skew));
2025 EXPECT_EQ(FLT_MAX, array2.UnalignedAt<float>(4 + skew));
2026
2027 memset(data, 0xAA, ARRAY_SIZE(data));
2028 array1.SetUnalignedAt<float>(0, 0.0f);
2029 EXPECT_EQ(0.0f, array2.UnalignedAt<float>(0 + skew));
2030 array2.SetUnalignedAt<float>(4 + skew, 1.0f);
2031 EXPECT_EQ(1.0f, array1.UnalignedAt<float>(4));
2032
2033 memset(data, 0xAA, ARRAY_SIZE(data));
2034 array1.SetUnalignedAt<double>(0, DBL_MIN);
2035 EXPECT_EQ(DBL_MIN, array2.UnalignedAt<double>(0 + skew));
2036 array2.SetUnalignedAt<double>(8 + skew, DBL_MAX);
2037 EXPECT_EQ(DBL_MAX, array1.UnalignedAt<double>(8));
2038
2039 memset(data, 0xAA, ARRAY_SIZE(data));
2040 array1.SetUnalignedAt<double>(0, 0.0);
2041 EXPECT_EQ(0.0, array2.UnalignedAt<double>(0 + skew));
2042 array2.SetUnalignedAt<double>(8 + skew, 1.0);
2043 EXPECT_EQ(1.0, array1.UnalignedAt<double>(8));
2044
2045 delete[] data;
2046 }
2047
2048
2049 TEST_CASE(ExternalByteArrayCopy) {
2050 const uint8_t b_0_1_2_3[] = { 0, 1, 2, 3 }; 1977 const uint8_t b_0_1_2_3[] = { 0, 1, 2, 3 };
2051 const uint8_t b_4_5_6_7[] = { 4, 5, 6, 7 }; 1978 const uint8_t b_4_5_6_7[] = { 4, 5, 6, 7 };
2052 1979
1980 const Uint8Array& internal =
1981 Uint8Array::Handle(Uint8Array::New(b_0_1_2_3, ARRAY_SIZE(b_0_1_2_3)));
1982 EXPECT(!internal.IsNull());
1983 EXPECT_EQ(4, internal.Length());
1984 EXPECT_EQ(0, internal.At(0));
1985 EXPECT_EQ(1, internal.At(1));
1986 EXPECT_EQ(2, internal.At(2));
1987 EXPECT_EQ(3, internal.At(3));
1988
1989 // A zero length copy.
1990 ByteArray::Copy(internal, 0, b_4_5_6_7, 0);
1991 EXPECT_EQ(0, internal.At(0));
1992 EXPECT_EQ(1, internal.At(1));
1993 EXPECT_EQ(2, internal.At(2));
1994 EXPECT_EQ(3, internal.At(3));
1995
1996 // Another zero length copy.
1997 ByteArray::Copy(internal, 4, b_4_5_6_7, 0);
1998 EXPECT_EQ(0, internal.At(0));
1999 EXPECT_EQ(1, internal.At(1));
2000 EXPECT_EQ(2, internal.At(2));
2001 EXPECT_EQ(3, internal.At(3));
2002
2003 // A one element copy.
2004 ByteArray::Copy(internal, 0, b_4_5_6_7, 1);
2005 EXPECT_EQ(4, internal.At(0));
2006 EXPECT_EQ(1, internal.At(1));
2007 EXPECT_EQ(2, internal.At(2));
2008 EXPECT_EQ(3, internal.At(3));
2009
2010 // A two element copy.
2011 ByteArray::Copy(internal, 2, b_4_5_6_7, 2);
2012 EXPECT_EQ(4, internal.At(0));
2013 EXPECT_EQ(1, internal.At(1));
2014 EXPECT_EQ(4, internal.At(2));
2015 EXPECT_EQ(5, internal.At(3));
2016
2017 // A three element copy.
2018 ByteArray::Copy(internal, 1, b_4_5_6_7, 3);
2019 EXPECT_EQ(4, internal.At(0));
2020 EXPECT_EQ(4, internal.At(1));
2021 EXPECT_EQ(5, internal.At(2));
2022 EXPECT_EQ(6, internal.At(3));
2023
2024 // A four element copy.
2025 ByteArray::Copy(internal, 0, b_0_1_2_3, 4);
2026 EXPECT_EQ(0, internal.At(0));
2027 EXPECT_EQ(1, internal.At(1));
2028 EXPECT_EQ(2, internal.At(2));
2029 EXPECT_EQ(3, internal.At(3));
2030 }
2031
2032
2033 TEST_CASE(ByteArrayCopyExternal) {
2034 const uint8_t b_0_1_2_3[] = { 0, 1, 2, 3 };
2035 const uint8_t b_4_5_6_7[] = { 4, 5, 6, 7 };
2036
2053 uint8_t data[] = { 0, 1, 2, 3 }; 2037 uint8_t data[] = { 0, 1, 2, 3 };
2054 const ExternalByteArray& external = 2038 const ExternalUint8Array& external =
2055 ExternalByteArray::Handle( 2039 ExternalUint8Array::Handle(
2056 ExternalByteArray::New(data, ARRAY_SIZE(data), NULL, NULL)); 2040 ExternalUint8Array::New(data, ARRAY_SIZE(data), NULL, NULL));
2057 EXPECT(!external.IsNull()); 2041 EXPECT(!external.IsNull());
2058 EXPECT_EQ(4, external.Length()); 2042 EXPECT_EQ(4, external.Length());
2059 EXPECT_EQ(0, external.At<uint8_t>(0)); 2043 EXPECT_EQ(0, external.At(0));
2060 EXPECT_EQ(1, external.At<uint8_t>(1)); 2044 EXPECT_EQ(1, external.At(1));
2061 EXPECT_EQ(2, external.At<uint8_t>(2)); 2045 EXPECT_EQ(2, external.At(2));
2062 EXPECT_EQ(3, external.At<uint8_t>(3)); 2046 EXPECT_EQ(3, external.At(3));
2063 2047
2064 // A zero length copy. 2048 // A zero length copy.
2065 ByteArray::Copy(external, 0, b_4_5_6_7, 0); 2049 ByteArray::Copy(external, 0, b_4_5_6_7, 0);
2066 EXPECT_EQ(0, external.At<uint8_t>(0)); 2050 EXPECT_EQ(0, external.At(0));
2067 EXPECT_EQ(1, external.At<uint8_t>(1)); 2051 EXPECT_EQ(1, external.At(1));
2068 EXPECT_EQ(2, external.At<uint8_t>(2)); 2052 EXPECT_EQ(2, external.At(2));
2069 EXPECT_EQ(3, external.At<uint8_t>(3)); 2053 EXPECT_EQ(3, external.At(3));
2070 2054
2071 // Another zero length copy. 2055 // Another zero length copy.
2072 ByteArray::Copy(external, 4, b_4_5_6_7, 0); 2056 ByteArray::Copy(external, 4, b_4_5_6_7, 0);
2073 EXPECT_EQ(0, external.At<uint8_t>(0)); 2057 EXPECT_EQ(0, external.At(0));
2074 EXPECT_EQ(1, external.At<uint8_t>(1)); 2058 EXPECT_EQ(1, external.At(1));
2075 EXPECT_EQ(2, external.At<uint8_t>(2)); 2059 EXPECT_EQ(2, external.At(2));
2076 EXPECT_EQ(3, external.At<uint8_t>(3)); 2060 EXPECT_EQ(3, external.At(3));
2077 2061
2078 // A one element copy. 2062 // A one element copy.
2079 ByteArray::Copy(external, 0, b_4_5_6_7, 1); 2063 ByteArray::Copy(external, 0, b_4_5_6_7, 1);
2080 EXPECT_EQ(4, external.At<uint8_t>(0)); 2064 EXPECT_EQ(4, external.At(0));
2081 EXPECT_EQ(1, external.At<uint8_t>(1)); 2065 EXPECT_EQ(1, external.At(1));
2082 EXPECT_EQ(2, external.At<uint8_t>(2)); 2066 EXPECT_EQ(2, external.At(2));
2083 EXPECT_EQ(3, external.At<uint8_t>(3)); 2067 EXPECT_EQ(3, external.At(3));
2084 2068
2085 // A two element copy. 2069 // A two element copy.
2086 ByteArray::Copy(external, 2, b_4_5_6_7, 2); 2070 ByteArray::Copy(external, 2, b_4_5_6_7, 2);
2087 EXPECT_EQ(4, external.At<uint8_t>(0)); 2071 EXPECT_EQ(4, external.At(0));
2088 EXPECT_EQ(1, external.At<uint8_t>(1)); 2072 EXPECT_EQ(1, external.At(1));
2089 EXPECT_EQ(4, external.At<uint8_t>(2)); 2073 EXPECT_EQ(4, external.At(2));
2090 EXPECT_EQ(5, external.At<uint8_t>(3)); 2074 EXPECT_EQ(5, external.At(3));
2091 2075
2092 // A three element copy. 2076 // A three element copy.
2093 ByteArray::Copy(external, 1, b_4_5_6_7, 3); 2077 ByteArray::Copy(external, 1, b_4_5_6_7, 3);
2094 EXPECT_EQ(4, external.At<uint8_t>(0)); 2078 EXPECT_EQ(4, external.At(0));
2095 EXPECT_EQ(4, external.At<uint8_t>(1)); 2079 EXPECT_EQ(4, external.At(1));
2096 EXPECT_EQ(5, external.At<uint8_t>(2)); 2080 EXPECT_EQ(5, external.At(2));
2097 EXPECT_EQ(6, external.At<uint8_t>(3)); 2081 EXPECT_EQ(6, external.At(3));
2098 2082
2099 // A four element copy. 2083 // A four element copy.
2100 ByteArray::Copy(external, 0, b_0_1_2_3, 4); 2084 ByteArray::Copy(external, 0, b_0_1_2_3, 4);
2101 EXPECT_EQ(0, external.At<uint8_t>(0)); 2085 EXPECT_EQ(0, external.At(0));
2102 EXPECT_EQ(1, external.At<uint8_t>(1)); 2086 EXPECT_EQ(1, external.At(1));
2103 EXPECT_EQ(2, external.At<uint8_t>(2)); 2087 EXPECT_EQ(2, external.At(2));
2104 EXPECT_EQ(3, external.At<uint8_t>(3)); 2088 EXPECT_EQ(3, external.At(3));
2105 } 2089 }
2106 2090
2107 2091
2108 TEST_CASE(InternalByteArray) { 2092 TEST_CASE(ByteArrayCopyInternalExternal) {
2109 uint8_t data[] = { 253, 254, 255, 0, 1, 2, 3, 4 };
2110 intptr_t data_length = ARRAY_SIZE(data);
2111
2112 const InternalByteArray& array1 =
2113 InternalByteArray::Handle(InternalByteArray::New(data, data_length));
2114 EXPECT(!array1.IsNull());
2115 EXPECT_EQ(data_length, array1.Length());
2116 EXPECT_EQ(-3, array1.At<int8_t>(0));
2117 EXPECT_EQ(253, array1.At<uint8_t>(0));
2118 EXPECT_EQ(-2, array1.At<int8_t>(1));
2119 EXPECT_EQ(254, array1.At<uint8_t>(1));
2120 EXPECT_EQ(-1, array1.At<int8_t>(2));
2121 EXPECT_EQ(255, array1.At<uint8_t>(2));
2122 EXPECT_EQ(0, array1.At<int8_t>(3));
2123 EXPECT_EQ(0, array1.At<uint8_t>(3));
2124 EXPECT_EQ(1, array1.At<int8_t>(4));
2125 EXPECT_EQ(1, array1.At<uint8_t>(4));
2126 EXPECT_EQ(2, array1.At<int8_t>(5));
2127 EXPECT_EQ(2, array1.At<uint8_t>(5));
2128
2129 const InternalByteArray& array2 =
2130 InternalByteArray::Handle(InternalByteArray::New(data, data_length));
2131 EXPECT(!array1.IsNull());
2132 EXPECT_EQ(data_length, array2.Length());
2133 for (intptr_t i = 0; i < array1.Length(); ++i) {
2134 EXPECT_EQ(array1.At<uint8_t>(i), array2.At<uint8_t>(i));
2135 }
2136
2137 array1.SetAt<uint8_t>(0, 123);
2138 array2.SetAt<int8_t>(2, -123);
2139
2140 EXPECT(array1.At<uint8_t>(0) != array2.At<uint8_t>(0));
2141 EXPECT(array1.At<uint8_t>(1) == array2.At<uint8_t>(1));
2142 EXPECT(array1.At<uint8_t>(2) != array2.At<uint8_t>(2));
2143 for (intptr_t i = 3; i < array1.Length(); ++i) {
2144 EXPECT(array1.At<uint8_t>(i) == array2.At<uint8_t>(i));
2145 }
2146 }
2147
2148
2149 TEST_CASE(InternalByteArrayAlignedAccess) {
2150 const intptr_t length = 16;
2151 const InternalByteArray& byte_array =
2152 InternalByteArray::Handle(InternalByteArray::New(length));
2153 EXPECT(!byte_array.IsNull());
2154 EXPECT_EQ(length, byte_array.Length());
2155
2156 byte_array.SetAt<float>(0, FLT_MIN);
2157 byte_array.SetAt<float>(4, FLT_MAX);
2158 EXPECT_EQ(FLT_MIN, byte_array.At<float>(0));
2159 EXPECT_EQ(FLT_MAX, byte_array.At<float>(4));
2160
2161 byte_array.SetAt<float>(0, 0.0f);
2162 EXPECT_EQ(0.0f, byte_array.At<float>(0));
2163 byte_array.SetAt<float>(4, 1.0f);
2164 EXPECT_EQ(1.0f, byte_array.At<float>(4));
2165
2166 byte_array.SetAt<double>(0, DBL_MIN);
2167 EXPECT_EQ(DBL_MIN, byte_array.At<double>(0));
2168 byte_array.SetAt<double>(8, DBL_MAX);
2169 EXPECT_EQ(DBL_MAX, byte_array.At<double>(8));
2170
2171 byte_array.SetAt<double>(0, 0.0);
2172 EXPECT_EQ(0.0, byte_array.At<double>(0));
2173 byte_array.SetAt<double>(8, 1.0);
2174 EXPECT_EQ(1.0, byte_array.At<double>(8));
2175 }
2176
2177
2178 TEST_CASE(InternalByteArrayUnlignedAccess) {
2179 const intptr_t length = 24;
2180 const InternalByteArray& byte_array =
2181 InternalByteArray::Handle(InternalByteArray::New(length));
2182 EXPECT(!byte_array.IsNull());
2183 EXPECT_EQ(length, byte_array.Length());
2184
2185 int float_misalign = 3;
2186 byte_array.SetUnalignedAt<float>(float_misalign, FLT_MIN);
2187 byte_array.SetUnalignedAt<float>(4 + float_misalign, FLT_MAX);
2188 EXPECT_EQ(FLT_MIN, byte_array.UnalignedAt<float>(float_misalign));
2189 EXPECT_EQ(FLT_MAX, byte_array.UnalignedAt<float>(4 + float_misalign));
2190
2191 byte_array.SetUnalignedAt<float>(float_misalign, 0.0f);
2192 EXPECT_EQ(0.0f, byte_array.UnalignedAt<float>(float_misalign));
2193 byte_array.SetUnalignedAt<float>(4 + float_misalign, 1.0f);
2194 EXPECT_EQ(1.0f, byte_array.UnalignedAt<float>(4 + float_misalign));
2195
2196 int double_misalign = 5;
2197 byte_array.SetUnalignedAt<double>(double_misalign, DBL_MIN);
2198 EXPECT_EQ(DBL_MIN, byte_array.UnalignedAt<double>(double_misalign));
2199 byte_array.SetUnalignedAt<double>(8 + double_misalign, DBL_MAX);
2200 EXPECT_EQ(DBL_MAX,
2201 byte_array.UnalignedAt<double>(8 + double_misalign));
2202
2203 byte_array.SetUnalignedAt<double>(double_misalign, 0.0);
2204 EXPECT_EQ(0.0, byte_array.UnalignedAt<double>(double_misalign));
2205 byte_array.SetUnalignedAt<double>(8 + double_misalign, 1.0);
2206 EXPECT_EQ(1.0, byte_array.UnalignedAt<double>(8 + double_misalign));
2207 }
2208
2209
2210 TEST_CASE(InternalByteArrayCopy) {
2211 const uint8_t b_0_1_2_3[] = { 0, 1, 2, 3 }; 2093 const uint8_t b_0_1_2_3[] = { 0, 1, 2, 3 };
2212 const uint8_t b_4_5_6_7[] = { 4, 5, 6, 7 }; 2094
2213 2095 const Uint8Array& internal =
2214 const InternalByteArray& internal = 2096 Uint8Array::Handle(Uint8Array::New(b_0_1_2_3, ARRAY_SIZE(b_0_1_2_3)));
2215 InternalByteArray::Handle(
2216 InternalByteArray::New(b_0_1_2_3, ARRAY_SIZE(b_0_1_2_3)));
2217 EXPECT(!internal.IsNull()); 2097 EXPECT(!internal.IsNull());
2218 EXPECT_EQ(4, internal.Length()); 2098 EXPECT_EQ(4, internal.Length());
2219 EXPECT_EQ(0, internal.At<uint8_t>(0)); 2099 EXPECT_EQ(0, internal.At(0));
2220 EXPECT_EQ(1, internal.At<uint8_t>(1)); 2100 EXPECT_EQ(1, internal.At(1));
2221 EXPECT_EQ(2, internal.At<uint8_t>(2)); 2101 EXPECT_EQ(2, internal.At(2));
2222 EXPECT_EQ(3, internal.At<uint8_t>(3)); 2102 EXPECT_EQ(3, internal.At(3));
2223
2224 // A zero length copy.
2225 ByteArray::Copy(internal, 0, b_4_5_6_7, 0);
2226 EXPECT_EQ(0, internal.At<uint8_t>(0));
2227 EXPECT_EQ(1, internal.At<uint8_t>(1));
2228 EXPECT_EQ(2, internal.At<uint8_t>(2));
2229 EXPECT_EQ(3, internal.At<uint8_t>(3));
2230
2231 // Another zero length copy.
2232 ByteArray::Copy(internal, 4, b_4_5_6_7, 0);
2233 EXPECT_EQ(0, internal.At<uint8_t>(0));
2234 EXPECT_EQ(1, internal.At<uint8_t>(1));
2235 EXPECT_EQ(2, internal.At<uint8_t>(2));
2236 EXPECT_EQ(3, internal.At<uint8_t>(3));
2237
2238 // A one element copy.
2239 ByteArray::Copy(internal, 0, b_4_5_6_7, 1);
2240 EXPECT_EQ(4, internal.At<uint8_t>(0));
2241 EXPECT_EQ(1, internal.At<uint8_t>(1));
2242 EXPECT_EQ(2, internal.At<uint8_t>(2));
2243 EXPECT_EQ(3, internal.At<uint8_t>(3));
2244
2245 // A two element copy.
2246 ByteArray::Copy(internal, 2, b_4_5_6_7, 2);
2247 EXPECT_EQ(4, internal.At<uint8_t>(0));
2248 EXPECT_EQ(1, internal.At<uint8_t>(1));
2249 EXPECT_EQ(4, internal.At<uint8_t>(2));
2250 EXPECT_EQ(5, internal.At<uint8_t>(3));
2251
2252 // A three element copy.
2253 ByteArray::Copy(internal, 1, b_4_5_6_7, 3);
2254 EXPECT_EQ(4, internal.At<uint8_t>(0));
2255 EXPECT_EQ(4, internal.At<uint8_t>(1));
2256 EXPECT_EQ(5, internal.At<uint8_t>(2));
2257 EXPECT_EQ(6, internal.At<uint8_t>(3));
2258
2259 // A four element copy.
2260 ByteArray::Copy(internal, 0, b_0_1_2_3, 4);
2261 EXPECT_EQ(0, internal.At<uint8_t>(0));
2262 EXPECT_EQ(1, internal.At<uint8_t>(1));
2263 EXPECT_EQ(2, internal.At<uint8_t>(2));
2264 EXPECT_EQ(3, internal.At<uint8_t>(3));
2265 }
2266
2267
2268 TEST_CASE(ByteArrayCopy) {
2269 const uint8_t b_0_1_2_3[] = { 0, 1, 2, 3 };
2270
2271 const InternalByteArray& internal =
2272 InternalByteArray::Handle(
2273 InternalByteArray::New(b_0_1_2_3, ARRAY_SIZE(b_0_1_2_3)));
2274 EXPECT(!internal.IsNull());
2275 EXPECT_EQ(4, internal.Length());
2276 EXPECT_EQ(0, internal.At<uint8_t>(0));
2277 EXPECT_EQ(1, internal.At<uint8_t>(1));
2278 EXPECT_EQ(2, internal.At<uint8_t>(2));
2279 EXPECT_EQ(3, internal.At<uint8_t>(3));
2280 2103
2281 uint8_t data[] = { 4, 5, 6, 7 }; 2104 uint8_t data[] = { 4, 5, 6, 7 };
2282 const ExternalByteArray& external = 2105 const ExternalUint8Array& external =
2283 ExternalByteArray::Handle( 2106 ExternalUint8Array::Handle(
2284 ExternalByteArray::New(data, ARRAY_SIZE(data), NULL, NULL)); 2107 ExternalUint8Array::New(data, ARRAY_SIZE(data), NULL, NULL));
2285 EXPECT(!external.IsNull()); 2108 EXPECT(!external.IsNull());
2286 EXPECT_EQ(4, external.Length()); 2109 EXPECT_EQ(4, external.Length());
2287 EXPECT_EQ(4, external.At<uint8_t>(0)); 2110 EXPECT_EQ(4, external.At(0));
2288 EXPECT_EQ(5, external.At<uint8_t>(1)); 2111 EXPECT_EQ(5, external.At(1));
2289 EXPECT_EQ(6, external.At<uint8_t>(2)); 2112 EXPECT_EQ(6, external.At(2));
2290 EXPECT_EQ(7, external.At<uint8_t>(3)); 2113 EXPECT_EQ(7, external.At(3));
2291 2114
2292 // A zero length copy. 2115 // A zero length copy.
2293 ByteArray::Copy(internal, 0, external, 0, 0); 2116 ByteArray::Copy(internal, 0, external, 0, 0);
2294 EXPECT_EQ(0, internal.At<uint8_t>(0)); 2117 EXPECT_EQ(0, internal.At(0));
2295 EXPECT_EQ(1, internal.At<uint8_t>(1)); 2118 EXPECT_EQ(1, internal.At(1));
2296 EXPECT_EQ(2, internal.At<uint8_t>(2)); 2119 EXPECT_EQ(2, internal.At(2));
2297 EXPECT_EQ(3, internal.At<uint8_t>(3)); 2120 EXPECT_EQ(3, internal.At(3));
2298 2121
2299 // A zero length copy, take 2. 2122 // A zero length copy, take 2.
2300 ByteArray::Copy(internal, 4, external, 0, 0); 2123 ByteArray::Copy(internal, 4, external, 0, 0);
2301 EXPECT_EQ(0, internal.At<uint8_t>(0)); 2124 EXPECT_EQ(0, internal.At(0));
2302 EXPECT_EQ(1, internal.At<uint8_t>(1)); 2125 EXPECT_EQ(1, internal.At(1));
2303 EXPECT_EQ(2, internal.At<uint8_t>(2)); 2126 EXPECT_EQ(2, internal.At(2));
2304 EXPECT_EQ(3, internal.At<uint8_t>(3)); 2127 EXPECT_EQ(3, internal.At(3));
2305 2128
2306 // A zero length copy, take 3. 2129 // A zero length copy, take 3.
2307 ByteArray::Copy(internal, 0, external, 4, 0); 2130 ByteArray::Copy(internal, 0, external, 4, 0);
2308 EXPECT_EQ(0, internal.At<uint8_t>(0)); 2131 EXPECT_EQ(0, internal.At(0));
2309 EXPECT_EQ(1, internal.At<uint8_t>(1)); 2132 EXPECT_EQ(1, internal.At(1));
2310 EXPECT_EQ(2, internal.At<uint8_t>(2)); 2133 EXPECT_EQ(2, internal.At(2));
2311 EXPECT_EQ(3, internal.At<uint8_t>(3)); 2134 EXPECT_EQ(3, internal.At(3));
2312 2135
2313 // A zero length copy, take 4. 2136 // A zero length copy, take 4.
2314 ByteArray::Copy(internal, 4, external, 4, 0); 2137 ByteArray::Copy(internal, 4, external, 4, 0);
2315 EXPECT_EQ(0, internal.At<uint8_t>(0)); 2138 EXPECT_EQ(0, internal.At(0));
2316 EXPECT_EQ(1, internal.At<uint8_t>(1)); 2139 EXPECT_EQ(1, internal.At(1));
2317 EXPECT_EQ(2, internal.At<uint8_t>(2)); 2140 EXPECT_EQ(2, internal.At(2));
2318 EXPECT_EQ(3, internal.At<uint8_t>(3)); 2141 EXPECT_EQ(3, internal.At(3));
2319 2142
2320 // A four element copy. 2143 // A four element copy.
2321 ByteArray::Copy(internal, 0, external, 0, 4); 2144 ByteArray::Copy(internal, 0, external, 0, 4);
2322 EXPECT_EQ(4, internal.At<uint8_t>(0)); 2145 EXPECT_EQ(4, internal.At(0));
2323 EXPECT_EQ(5, internal.At<uint8_t>(1)); 2146 EXPECT_EQ(5, internal.At(1));
2324 EXPECT_EQ(6, internal.At<uint8_t>(2)); 2147 EXPECT_EQ(6, internal.At(2));
2325 EXPECT_EQ(7, internal.At<uint8_t>(3)); 2148 EXPECT_EQ(7, internal.At(3));
2326 EXPECT_EQ(4, external.At<uint8_t>(0)); 2149 EXPECT_EQ(4, external.At(0));
2327 EXPECT_EQ(5, external.At<uint8_t>(1)); 2150 EXPECT_EQ(5, external.At(1));
2328 EXPECT_EQ(6, external.At<uint8_t>(2)); 2151 EXPECT_EQ(6, external.At(2));
2329 EXPECT_EQ(7, external.At<uint8_t>(3)); 2152 EXPECT_EQ(7, external.At(3));
2330 2153
2331 // A four element copy, take 2. 2154 // A four element copy, take 2.
2332 ByteArray::Copy(external, 0, b_0_1_2_3, 4); 2155 ByteArray::Copy(external, 0, b_0_1_2_3, 4);
2333 EXPECT_EQ(0, external.At<uint8_t>(0)); 2156 EXPECT_EQ(0, external.At(0));
2334 EXPECT_EQ(1, external.At<uint8_t>(1)); 2157 EXPECT_EQ(1, external.At(1));
2335 EXPECT_EQ(2, external.At<uint8_t>(2)); 2158 EXPECT_EQ(2, external.At(2));
2336 EXPECT_EQ(3, external.At<uint8_t>(3)); 2159 EXPECT_EQ(3, external.At(3));
2337 ByteArray::Copy(external, 0, internal, 0, 4); 2160 ByteArray::Copy(external, 0, internal, 0, 4);
2338 EXPECT_EQ(4, internal.At<uint8_t>(0)); 2161 EXPECT_EQ(4, internal.At(0));
2339 EXPECT_EQ(5, internal.At<uint8_t>(1)); 2162 EXPECT_EQ(5, internal.At(1));
2340 EXPECT_EQ(6, internal.At<uint8_t>(2)); 2163 EXPECT_EQ(6, internal.At(2));
2341 EXPECT_EQ(7, internal.At<uint8_t>(3)); 2164 EXPECT_EQ(7, internal.At(3));
2342 EXPECT_EQ(4, external.At<uint8_t>(0)); 2165 EXPECT_EQ(4, external.At(0));
2343 EXPECT_EQ(5, external.At<uint8_t>(1)); 2166 EXPECT_EQ(5, external.At(1));
2344 EXPECT_EQ(6, external.At<uint8_t>(2)); 2167 EXPECT_EQ(6, external.At(2));
2345 EXPECT_EQ(7, external.At<uint8_t>(3)); 2168 EXPECT_EQ(7, external.At(3));
2346 } 2169 }
2347 2170
2348 2171
2349 TEST_CASE(Script) { 2172 TEST_CASE(Script) {
2350 const char* url_chars = "builtin:test-case"; 2173 const char* url_chars = "builtin:test-case";
2351 const char* source_chars = "This will not compile."; 2174 const char* source_chars = "This will not compile.";
2352 const String& url = String::Handle(String::New(url_chars)); 2175 const String& url = String::Handle(String::New(url_chars));
2353 const String& source = String::Handle(String::New(source_chars)); 2176 const String& source = String::Handle(String::New(source_chars));
2354 const Script& script = Script::Handle(Script::New(url, 2177 const Script& script = Script::Handle(Script::New(url,
2355 source, 2178 source,
2356 RawScript::kSource)); 2179 RawScript::kSource));
2357 EXPECT(!script.IsNull()); 2180 EXPECT(!script.IsNull());
2358 EXPECT(script.IsScript()); 2181 EXPECT(script.IsScript());
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after
2822 cache.GetCheck(0, &test_class, &test_targ_0, &test_targ_1, &test_result); 2645 cache.GetCheck(0, &test_class, &test_targ_0, &test_targ_1, &test_result);
2823 EXPECT_EQ(empty_class.raw(), test_class.raw()); 2646 EXPECT_EQ(empty_class.raw(), test_class.raw());
2824 EXPECT_EQ(targ_0.raw(), test_targ_0.raw()); 2647 EXPECT_EQ(targ_0.raw(), test_targ_0.raw());
2825 EXPECT_EQ(targ_1.raw(), test_targ_1.raw()); 2648 EXPECT_EQ(targ_1.raw(), test_targ_1.raw());
2826 EXPECT_EQ(Bool::True(), test_result.raw()); 2649 EXPECT_EQ(Bool::True(), test_result.raw());
2827 } 2650 }
2828 2651
2829 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 2652 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
2830 2653
2831 } // namespace dart 2654 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698