/* This tests if it returns the correct list of keys and vals */ U8 Dict_test2(void) { dict_keys(p_dict, &p_list); list_getItem(p_list, 0, &p_tempobj); if (object_isEqual(p_tempobj,(pPmObj_t) p_firstkey)==0) return 0 ; list_getItem(p_list, 1, &p_tempobj); if (object_isEqual(p_tempobj,(pPmObj_t) p_secondkey)==0) return 0 ; list_getItem(p_list, 2, &p_tempobj); if (object_isEqual(p_tempobj,(pPmObj_t) p_thirdkey)==0) return 0 ; //destroy_chunk(p_list); //destroys elements in p_list. Destroys contents the keys! bad dict_vals(p_dict, &p_list); list_getItem(p_list, 0, &p_tempobj); if (object_isEqual(p_tempobj,(pPmObj_t) p_firstval)==0) return 0 ; list_getItem(p_list, 1, &p_tempobj); if (object_isEqual(p_tempobj,(pPmObj_t) p_secondval)==0) return 0 ; list_getItem(p_list, 2, &p_tempobj); if (object_isEqual(p_tempobj,(pPmObj_t) p_thirdval)==0) return 0 ; //NO. Destroys contents. We want to reuse for other tests. //destroy_chunk(p_list); //destroy_chunk(p_tempobj); return 1; }
/* Returns the object sequence[index] */ PmReturn_t seq_getSubscript(pPmObj_t pobj, int16_t index, pPmObj_t *r_pobj) { PmReturn_t retval; uint8_t c; switch (OBJ_GET_TYPE(pobj)) { case OBJ_TYPE_STR: /* Adjust for negative index */ if (index < 0) { index += ((pPmString_t)pobj)->length; } /* Raise IndexError if index is out of bounds */ if ((index < 0) || (index > ((pPmString_t)pobj)->length)) { PM_RAISE(retval, PM_RET_EX_INDX); break; } /* Get the character from the string */ c = ((pPmString_t)pobj)->val[index]; /* Create a new string from the character */ retval = string_newFromChar(c, r_pobj); break; case OBJ_TYPE_TUP: /* Get the tuple item */ retval = tuple_getItem(pobj, index, r_pobj); break; case OBJ_TYPE_LST: /* Get the list item */ retval = list_getItem(pobj, index, r_pobj); break; default: /* Raise TypeError, unsubscriptable object */ PM_RAISE(retval, PM_RET_EX_TYPE); break; } return retval; }
/* Returns true if the item is in the container object */ PmReturn_t obj_isIn(pPmObj_t pobj, pPmObj_t pitem) { PmReturn_t retval = PM_RET_NO; pPmObj_t ptestItem; int16_t i; uint8_t c; switch (OBJ_GET_TYPE(pobj)) { case OBJ_TYPE_TUP: /* Iterate over tuple to find item */ for (i = 0; i < ((pPmTuple_t)pobj)->length; i++) { PM_RETURN_IF_ERROR(tuple_getItem(pobj, i, &ptestItem)); if (obj_compare(pitem, ptestItem) == C_SAME) { retval = PM_RET_OK; break; } } break; case OBJ_TYPE_STR: /* Raise a TypeError if item is not a string */ if ((OBJ_GET_TYPE(pitem) != OBJ_TYPE_STR)) { retval = PM_RET_EX_TYPE; break; } /* Empty string is alway present */ if (((pPmString_t)pitem)->length == 0) { retval = PM_RET_OK; break; } /* Raise a ValueError if the string is more than 1 char */ else if (((pPmString_t)pitem)->length != 1) { retval = PM_RET_EX_VAL; break; } /* Iterate over string to find char */ c = ((pPmString_t)pitem)->val[0]; for (i = 0; i < ((pPmString_t)pobj)->length; i++) { if (c == ((pPmString_t)pobj)->val[i]) { retval = PM_RET_OK; break; } } break; case OBJ_TYPE_LST: /* Iterate over list to find item */ for (i = 0; i < ((pPmList_t)pobj)->length; i++) { PM_RETURN_IF_ERROR(list_getItem(pobj, i, &ptestItem)); if (obj_compare(pitem, ptestItem) == C_SAME) { retval = PM_RET_OK; break; } } break; case OBJ_TYPE_DIC: /* Check if the item is one of the keys of the dict */ retval = dict_getItem(pobj, pitem, &ptestItem); if (retval == PM_RET_EX_KEY) { retval = PM_RET_NO; } break; default: retval = PM_RET_EX_TYPE; break; } return retval; }