comparison truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/Schema.java @ 22495:aeba89e1d8da

Add ComplexNumber sequence tests
author Matthias Grimmer <grimmer@ssw.jku.at>
date Fri, 11 Dec 2015 15:20:27 +0100
parents
children b3569a53c24c
comparison
equal deleted inserted replaced
22489:28227895fa35 22495:aeba89e1d8da
1 /*
2 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.oracle.truffle.tck;
26
27 import java.nio.ByteBuffer;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 public final class Schema {
33
34 public enum Type {
35 DOUBLE(Double.SIZE / Byte.SIZE),
36 INT(Integer.SIZE / Byte.SIZE);
37
38 private final int size;
39
40 private Type(int size) {
41 this.size = size;
42 }
43 }
44
45 private final int size;
46 private final boolean rowBased;
47 private final List<String> names;
48 private final List<Type> types;
49
50 public Schema(int size, boolean rowBased, List<String> names, List<Type> types) {
51 this.size = size;
52 this.rowBased = rowBased;
53 this.names = names;
54 this.types = types;
55 }
56
57 public int length() {
58 return size;
59 }
60
61 // for simplicity: structured data is read-only
62
63 public Map<String, Object> getEntry(byte[] buffer, int index) {
64 Map<String, Object> entry = new HashMap<>();
65 for (int i = 0; i < names.size(); i++) {
66 String name = names.get(i);
67 Object value = get(buffer, index, name);
68 entry.put(name, value);
69 }
70 return entry;
71 }
72
73 private Object get(byte[] buffer, int index, String name) {
74 assert names.contains(name);
75 int offset = rowBased ? getRowOffset(name, index) : getColumnOffset(name, index);
76 if (types.get(names.indexOf(name)) == Type.DOUBLE) {
77 byte[] b = new byte[Type.DOUBLE.size];
78 for (int i = 0; i < Type.DOUBLE.size; i++) {
79 b[i] = buffer[offset + i];
80 }
81 return ByteBuffer.wrap(b).getDouble();
82 } else if (types.get(names.indexOf(name)) == Type.INT) {
83 byte[] b = new byte[Type.INT.size];
84 for (int i = 0; i < Type.INT.size; i++) {
85 b[i] = buffer[offset + i];
86 }
87 return ByteBuffer.wrap(b).getInt();
88 }
89 throw new IllegalStateException();
90 }
91
92 private int getRowSize() {
93 assert rowBased;
94 int rowSize = 0;
95 for (Type t : types) {
96 rowSize += t.size;
97 }
98 return rowSize;
99 }
100
101 private int getRowOffset(String name, int index) {
102 assert rowBased;
103 if (names.contains(name)) {
104 int offset = 0;
105 for (int i = 0; i < names.size(); i++) {
106 if (names.get(i).equals(name)) {
107 return index * getRowSize() + offset;
108 } else {
109 offset += types.get(i).size;
110 }
111 }
112 } else {
113 throw new IllegalArgumentException();
114 }
115 throw new IllegalStateException();
116 }
117
118 private int getColumnOffset(String name, int index) {
119 assert !rowBased;
120 if (names.contains(name)) {
121 int offset = 0;
122 for (int i = 0; i < names.size(); i++) {
123 if (names.get(i).equals(name)) {
124 return offset + index * types.get(i).size;
125 } else {
126 offset += types.get(i).size * size;
127 }
128 }
129 } else {
130 throw new IllegalArgumentException();
131 }
132 throw new IllegalStateException();
133 }
134 }