### Question:
TupleDesc implements Serializable { public int fieldNameToIndex(String name) throws NoSuchElementException { for(int i=0;i<items.size();i++){ if(items.get(i).fieldName.equals(name)){ return i; } } throw new NoSuchElementException(); } TupleDesc(Type[] typeAr, String[] fieldAr);  TupleDesc(Type[] typeAr); Iterator<TDItem> iterator(); int numFields(); String getFieldName(int i); Type getFieldType(int i); int fieldNameToIndex(String name); int getSize(); static TupleDesc merge(TupleDesc td1, TupleDesc td2); boolean equals(Object o); int hashCode(); String toString(); public List<TDItem> items; }### Answer:
@Test public void nameToId() { int[] lengths = new int[] { 1, 2, 1000 }; String prefix = "test"; for (int len: lengths) { TupleDesc td = Utility.getTupleDesc(len, prefix); for (int i = 0; i < len; ++i) { assertEquals(i, td.fieldNameToIndex(prefix + i)); } try { td.fieldNameToIndex("foo"); Assert.fail("foo is not a valid field name"); } catch (NoSuchElementException e) { } try { td.fieldNameToIndex(null); Assert.fail("null is not a valid field name"); } catch (NoSuchElementException e) { } td = Utility.getTupleDesc(len); try { td.fieldNameToIndex(prefix); Assert.fail("no fields are named, so you can't find it"); } catch (NoSuchElementException e) { } } }